Which commits am I currently merging?

℡╲_俬逩灬. 提交于 2019-12-09 00:48:36

问题


When I executegit merge, no new commit are created yet (because of conflicts or --no-commit option), how to check which branch I am merging?

git status shows only current branch.


回答1:


A git merge that has stopped due to conflicts leaves two1 files with information about what is being merged:

.git/MERGE_HEAD
.git/MERGE_MSG

(replace .git with the output of git rev-parse --git-dir if needed, e.g., if you're in a sub-directory within the git directory). The contents of MERGE_HEAD is the other SHA-1, even if you're merging a branch by name;2 but the contents of MERGE_MSG contains the branch name if there is one. For instance:

$ git merge --no-commit origin/master
Automatic merge went well; stopped before committing as requested
$ cat .git/MERGE_HEAD
d1574b852963482d4b482992ad6343691082412f
$ cat .git/MERGE_MSG
Merge remote-tracking branch 'origin/master'

(I had to use --no-commit here since there was no conflict).

(If there is no branch name in either file, but the MERGE_HEAD file's SHA-1 matches some existing branch tip, that would be a suitable branch name for many or most purposes. If it matches multiple branch tips—i.e., if a single commit is the tip of several current branch names—any of those could be suitable.)


1There's a third file, .git/MERGE_MODE, which tells git that you're in the middle of a merge in the first place. It's often empty, though.

2You can merge by raw SHA-1, or any other way to specify a commit. There's no requirement that the merge be from another branch. For that matter, you can be in "detached HEAD" mode when making a new merge commit as well, so there's no requirement that the merge be into a named branch.




回答2:


There is a simple one-liner to find this out, and it works even if you're in a subdirectory and/or in a git worktree created by the git worktree command:

git rev-parse MERGE_HEAD


来源:https://stackoverflow.com/questions/33911218/which-commits-am-i-currently-merging

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!