孤儿提交属于哪个分支? (Git, Github)
好吧,根据定义,孤立提交不属于任何分支(顺便说一句,这样说有点错误,更好的措辞是“哪些分支提示提交在其层次结构链中具有特定提交?” em>),所以我想真正的问题是......
有什么方法可以将这些提交合并到我选择的另一个分支中?
确实如此。您可以使用git cherry-pick 命令,quoting the docs,“应用一些现有提交引入的更改”。这是一个如何使用它的简单示例:
首先,初始化存储库:
> git init
> echo 1 > first.txt
> git add . && git commit -m "First commit"
[master (root-commit) a8871cd] First commit
1 file changed, 1 insertion(+)
create mode 100644 first.txt
然后,创建孤儿提交(最简单的方法是创建一个分支,将其推进一点,然后将其删除):
> git checkout -b branch-to-remove
Switched to a new branch 'branch-to-remove'
> echo 1 > cherry.txt
> git add . && git commit -m "Cherry commit 1"
[branch-to-remove 7cd90f8] Cherry commit 1
1 file changed, 1 insertion(+)
create mode 100644 cherry.txt
> echo 2 >> first.txt
> git add . && git commit -m "Cherry commit 2"
[branch-to-remove 8289dee] Cherry commit 2
1 file changed, 1 insertion(+)
> git checkout master
> git branch -D branch-to-remove
Deleted branch branch-to-remove (was 8289dee).
所以分支消失了,但两个提交 - 7cd90f8 和 8289dee - 仍然存在。现在 master 分支开始工作了:
> echo 3 >> first.txt
> git add . && git commit -m "Second commit"
[master e0c199e] Second commit
1 file changed, 1 insertion(+)
现在我们遇到了一种情况:在某处应用了一些更改,我们需要将这些更改应用到当前分支。使用简单的非冲突更改是微不足道的,例如 Cherry commit 1 中介绍的更改:
> git cherry-pick 7cd90f8
[master 864dcbe] Cherry commit 1
1 file changed, 1 insertion(+)
create mode 100644 cherry.txt
重放选择的提交,应用更改,创建另一个提交,master 分支的尖端是先进的,生活是美好的。但是下一个会发生什么?
> git cherry-pick 8289
error: could not apply 8289dee... Cherry commit 2
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
事实上,没有什么不好的:因为两个提交 - HEAD 和选择的一个 - 影响了同一个文件,那里有一个经典的冲突。易于检查,易于修复;只是不要忘记git add所有生成的文件,稍后再git commit。
请注意,cherry-pick 结果的提交只有单亲(在这种情况下,它们类似于 rebase 提交)。