我昨天合并了两个分支机构。在合并期间,我意外地隐藏了一些更改,同时解决了合并冲突。
我刚刚意识到现在Git / SourceTree现在没有将merge-commit显示为合并,因为它没有合并提交的图形链接/行。
我不确定这是怎么发生的,如果是由于上面提到的藏匿或我代表的其他错误。
由于合并我已经付出一些努力来修复构建错误等,所以我不想重新启动。
所以我的问题是:有没有办法解决这个问题?!
答案 0 :(得分:3)
如果您的查看者准确地向您显示提交,并且未将其显示为合并提交,那么它确实不是合并提交。
以相反的顺序(与Git相似):
所以我的问题是:有没有办法解决这个问题?!
是。如有必要,通过提交保存所需的合并后结果。保存所需的任何其他内容(例如,根据需要进行其他提交)。我们这样做因为提交大多是永久性的,即我们能够让它们恢复。
然后,使用git reset --hard <hash>
将当前分支名称移回到一切出错之前的点,然后重复合并。使用保存的结果设置所需的合并结果,并提交它。如果需要,继续复制其他提交(使用git cherry-pick
)。请参阅下面的示例(在“恢复”部分下)。
我昨天合并了两个分支机构。在合并期间,我意外地隐藏了一些更改,同时解决了合并冲突。
这应该是不可能的 - 除非,即你已将所有文件标记为已解决。为了演示,我设置了合并冲突并运行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
让我们将其标记为已解决(同时将所有冲突标记留在其中)并藏匿并查看是否会破坏我们的合并状态:
$ 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
确实,进行两次存储提交并执行git reset
(git stash
确实)已经破坏了合并状态。应用和删除存储不会恢复合并状态:
$ 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")
现在,从未提交的合并前进一切都出错了。让我们快速了解一下我们现在拥有的东西(只要它足够显示,您就可以使用自己喜欢的观众):
$ git log --all --decorate --oneline --graph
* 33773a5 (HEAD -> master) conflicting change on master
| * c46f563 (sidebr) conflicting-change-on-sidebr
|/
* f23af46 mergebase
* d588ca0 initial
我们刚刚看到我们没有提交“好”的最终合并结果(它不是很好所以让我们放入一个演示版本),所以我们现在应该提交:
$ 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条目保存(使用{{ 1}}看到它们,我们已经记下了密钥哈希ID,或者复制到另一个窗口:
git reflog
现在我们准备好这次正确地重新进行合并:
$ 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
“好结果”在我们保存的ID的提交中,因此提取它们。只要您处于树的顶层,就可以使用$ git merge sidebr
Auto-merging file
CONFLICT (content): Merge conflict in file
Automatic merge failed; fix conflicts and then commit the result.
,并且此命令更容易记住,但我将使用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
所以我们在这里完成,除非你有额外的提交,在这种情况下你现在可以使用[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
在新的合并后将它们复制到位(在我的情况下,新的合并具有短的哈希ID { {1}}但你的当然会有所不同。)