【发布时间】:2026-01-22 04:55:02
【问题描述】:
对于我的 GitHub 页面,我使用静态站点生成器。该站点的源代码位于<source> 分支中,它在_site 目录中生成html 文件。<master> 分支用于显示实际网站,应包含根级别生成的 html 文件。
由于我不希望_site 目录及其内容成为我的<source> 分支的一部分,因此我忽略了_site 目录(通过将_site 放在我的.gitignore 中)。
现在我的问题是如何将<source> 分支的_site 目录的未跟踪内容合并到<master> 分支的根目录?
更新
主要问题似乎是 Git 需要跟踪您想要合并的任何内容(理所当然;)),但我不想在我的 <source> 分支中跟踪生成的文件。
我能想出的唯一解决方法是使用至少 1 个可以在之后删除的临时分支。使用 git subtree 从一个临时分支拆分到另一个临时分支似乎是最干净的选择。
有没有更好/更清洁的方法来做到这一点?
这是我想出的:
# Checkout branch <temp1> carrying over the current working
# directory including ignored/untracked files
git checkout -b temp1
# Add _site to staging ignoring .gitignore and commit
git add -f _site/
git commit -m "irrelevant commitmessage"
# Subtree split _site to <temp2> branch (root of branch)
git subtree --prefix=_site/ split -b temp2
# Checkout master and merge <temp2>
git checkout master
git merge --squash temp2
git commit -m "commitmessage"
# Remove <temp1> and <temp2> branches and push
【问题讨论】:
-
一种简单的方法,可能只是
checkout master,并且由于该分支将没有 .gitignore 条目,它会将站点下的所有更改文件显示为新文件或新更改。你能试试吗?这不是合并,但可能会成功。 -
@bitoiu 这是将未暂存/未跟踪文件移至另一个分支的最简单方法,但它们仍保留在其原始位置(即
<_site>文件夹)而不是分支的根目录中。由于我想仅将生成的文件合并到<master>分支(以保持站点的“已发布”版本之间的更改/差异),我需要找到一种方法来获取文件_site到分支的根目录,并且只将这些文件添加到提交中。 -
Git map directory to branch 的可能重复项
标签: git github merge github-pages