什么分支孤儿提交属于? (Git,Github)

时间:2016-01-13 03:03:38

标签: git github git-branch git-commit

我的Github回购中有一些 orphan 提交。它们不存在于任何分支或我的本地仓库中,如果我选择相关提交列表中的特定问题,我只能看到它们。

有没有办法可以将这些提交合并到我选择的另一个分支中?

enter image description here

1 个答案:

答案 0 :(得分:0)

  

什么分支孤儿提交属于? (Git,Github)

好吧,根据定义,孤立提交不属于任何分支(顺便说一句,说这个有点不对,更好的措辞是'哪些分支提交具有特定的提交在他们的等级链?' ),所以我想真正的问题是......

  

有没有办法可以将这些提交合并到我选择的另一个分支中?

肯定是的。您可以使用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 - 仍然存在。现在主分支正在进行:

> 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

重放提取的提交,应用更改,创建另一个提交,提高主分支的提示,生活是好的。但下一个会发生什么?

> 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引起的提交仅具有单个父级(在这种情况下,它们与重新提交的提交相似)。

相关问题