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.
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.
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.
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