Git merge 提交未在 Git/SourceTree 中显示为 merge

标签 git merge

我昨天 merge 了两个分支。在 merge 过程中,我不小心 stash 了一些更改,同时解决了 merge 冲突。

我刚刚意识到现在 Git/SourceTree 没有将 merge 提交显示为 merge ,因为它没有指向 merge 提交的图形链接/行。

我不确定这是怎么发生的,是否是由于上述 stash 或代表我的其他一些错误。

自 merge 以来,我已经付出了一些努力来修复构建错误等,所以我不想重新启动。

所以我的问题是:有什么方法可以解决这个问题吗?!

最佳答案

如果您的查看器准确地向您显示了提交,并且没有将其显示为 merge 提交,那么它确实不是 merge 提交。

以相反的顺序(很像 Git):

So my question is: Is there some way to fix this?!

是的。如有必要,通过提交保存所需的 merge 后结果。保存任何其他必要的东西(例如,根据需要进行额外的提交)。我们这样做是因为提交大多是永久性的,也就是说,我们将能够取回它们。

然后,使用 git reset --hard <hash>将当前分支名称移回一切出错之前的位置,然后重复 merge 。使用保存的结果设置所需的 merge 结果,并提交。如果需要,继续复制其他提交(使用 git cherry-pick )。请参阅下面的示例(在“恢复”部分下)。

I merged two branches yesterday. During merging I accidentally stashed some changes, while resolving merge conflicts.

这应该是不可能的——除非,也就是说,您将所有文件都标记为已解决。为了演示,我设置了一个 merge 冲突并运行了 git stash :

$ git merge sidebr
Auto-merging file
CONFLICT (content): Merge conflict in file
Automatic merge failed; fix conflicts and then commit the result.
[elf.24] T> git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both modified:   file

no changes added to commit (use "git add" and/or "git commit -a")
$ git stash
file: needs merge
file: needs merge
file: unmerged (3b0f0fbe60a4e91d6bc2b714ecaf6b7626067fbb)
file: unmerged (94101158b0ecff92a135ff928f9497c9d8ac8c16)
file: unmerged (70f0a490221b56a23f0879634b84cb6b1f9166c5)
fatal: git-write-tree: error building trees
Cannot save the current index state
$ 

冲突文件如下,仅供引用:

$ cat file
file for making a merge conflict
<<<<<<< HEAD
this is the second line
||||||| merged common ancestors
line 2
=======
line two
>>>>>>> sidebr

让我们将其标记为已解决(同时将所有冲突标记留在其中)并 stash 起来,看看这是否会破坏我们的 merge 状态:

$ git add file
$ git status
On branch master
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:

        modified:   file

$ git stash
Saved working directory and index state WIP on master: 33773a5 conflicting change on master
$ git status
On branch master
nothing to commit, working tree clean

确实,进行两次 stash 提交并执行 git reset (git stash 所做的) 破坏了 merge 状态。应用和删除存储不会恢复 merge 状态:

$ git stash apply
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   file

no changes added to commit (use "git add" and/or "git commit -a")
$ git stash drop
Dropped refs/stash@{0} (187eaa6e372592f697ea7f0dc43056aadb8b7a18)
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   file

no changes added to commit (use "git add" and/or "git commit -a")

正在恢复

现在,从未提交的 merge 向前开始,一切都出错了。让我们快速浏览一下我们现在有什么(您可以使用您喜欢的查看器,只要它显示足够多):

$ git log --all --decorate --oneline --graph
* 33773a5 (HEAD -> master) conflicting change on master
| * c46f563 (sidebr) conflicting-change-on-sidebr
|/  
* f23af46 mergebase
* d588ca0 initial

我们刚刚看到我们还没有提交“好”的最终 merge 结果(它不是很好所以让我们放一个演示版本),所以我们现在应该提交:

$ cat << end > file
> file for making a merge conflict
> which is now resolved
> end
$ git add file
$ git commit -m merged-but-not-merged
[master ba3208a] merged-but-not-merged
 Date: Fri Jan 12 07:38:16 2018 -0800
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git show
commit ba3208a75c3f4132d32b2d258fc8d4f1582e5837 (HEAD -> master)
Author: Chris Torek <chris.torek@gmail.com>
Date:   Fri Jan 12 07:38:16 2018 -0800

    merged-but-not-merged

diff --git a/file b/file
index 9410115..14e570a 100644
--- a/file
+++ b/file
@@ -1,2 +1,2 @@
 file for making a merge conflict
-this is the second line
+which is now resolved

解析数据的哈希 ID 是 ba3208a75c3f4132d32b2d258fc8d4f1582e5837 — 简短版本也足够了 — 一切都很干净:

# help from A DOG: All Decorate Oneline Graph
$ git log --all --decorate --oneline --graph
* ba3208a (HEAD -> master) merged-but-not-merged
* 33773a5 conflicting change on master
| * c46f563 (sidebr) conflicting-change-on-sidebr
|/  
* f23af46 mergebase
* d588ca0 initial
$ git status
On branch master
nothing to commit, working tree clean

我们现在准备使用git reset --hard ,这将似乎 丢失我们所有的工作,但实际上这些提交仍在 Git 中,由它们的 reflog 条目保存(使用 git reflog 查看它们)并且我们已经记下或复制到另一个窗口, key 哈希 ID:

$ git reset --hard 33773a5
HEAD is now at 33773a5 conflicting change on master
$ git log --all --decorate --oneline --graph
* 33773a5 (HEAD -> master) conflicting change on master
| * c46f563 (sidebr) conflicting-change-on-sidebr
|/  
* f23af46 mergebase
* d588ca0 initial

现在我们准备重新进行 merge ,这次是正确的:

$ git merge sidebr
Auto-merging file
CONFLICT (content): Merge conflict in file
Automatic merge failed; fix conflicts and then commit the result.

“好的结果”在我们保存其 ID 的提交中,所以提取它们。您可以使用 git checkout <hash> -- .只要你在树的顶层,这个命令就更容易记住,尽管我会使用 git read-tree --reset -u ,这也避免了通过删除解决的文件偶尔出现的棘手问题:

$ git read-tree --reset -u ba3208a  # or git checkout ba3208a -- .
$ git status
On branch master
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:

        modified:   file

$ git commit

此时,您选择的编辑器出现,这样您就可以构建比默认编辑器更好的 merge 消息,但我保留默认编辑器并退出编辑器:

[master e097ef5] Merge branch 'sidebr'
$ git log --all --decorate --oneline --graph
*   e097ef5 (HEAD -> master) Merge branch 'sidebr'
|\  
| * c46f563 (sidebr) conflicting-change-on-sidebr
* | 33773a5 conflicting change on master
|/  
* f23af46 mergebase
* d588ca0 initial
$ cat file
file for making a merge conflict
which is now resolved

所以我们到这里就完成了,除非您有额外的提交,在这种情况下您现在可以使用 git cherry-pick在新 merge 后将它们复制到位(在我的例子中,新 merge 具有短哈希 ID e097ef5 但你的当然会有所不同)。

关于Git merge 提交未在 Git/SourceTree 中显示为 merge ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48223068/

相关文章:

r - 在R中非常大的数据集中,将2列多次组合成1列

python - 在 Python 中将文件和差异合并到一个新文件中

json - 使用jq根据键值合并两个json值

audio - Swift:合并音频和视频文件

git - 从现有存储库中创建孤立分支?

git - 暂存具有特定扩展名的文件,并且只有那些在 git status 中显示为已修改的文件

git - git push 命令中的用户名和密码

git - Intellij 2020.1 显示 VCS 更改的快捷方式(模态提交对话)

node.js - 当我提交时如何获取 git commit 消息?

python - 合并多个数据框pandas