Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if Git merge is in process

Tags:

Is there a Git command that can be used to determine if a merge is in-process (i.e. uncommitted)? I know I can simply check for .git/MERGE_HEAD, but is this proper and/or future-proof for command-line scripts?

like image 479
pathfinderelite Avatar asked Jun 09 '15 13:06

pathfinderelite


People also ask

How do you check if there is merge conflict?

To see the beginning of the merge conflict in your file, search the file for the conflict marker <<<<<<< . When you open the file in your text editor, you'll see the changes from the HEAD or base branch after the line <<<<<<< HEAD .

Which command could you use to confirm your merge was successful?

Since MERGE_HEAD is only available during a merge and is also future-proof, we can simply use git rev-list -1 MERGE_HEAD (conveniently, a plumbing command) which will exit with code 128 if MERGE_HEAD is not found.

How does git determine merge conflicts?

For simple text files, Git uses an approach known as the longest common subsequence algorithm to perform merges and to detect merge conflicts. In its simplest form, Git find the longest set of lines in common between your changed file and the common ancestor.


Video Answer


1 Answers

One trick is to use a Git command that will fail if a merge is in progress. You can then check the return code of the command. You'll also want to make sure that the command will not affect your working copy or index in the event that it succeeds. While there are many commands that fall into this category, one that seems appropriate is

git merge HEAD 

which returns a 128 code if a merge is in progress, but 0 otherwise. Note that when a merge is not in-process, this command will simply print Already up-to-date since you are just merging with yourself. So, for scripting purposes, you can do this (in BASH)

git merge HEAD &> /dev/null result=$? if [ $result -ne 0 ] then     echo "Merge in progress." else     echo "Merge not in progress." fi 

Note that even with the --quiet flag of git merge, an in-process merge will still cause this command to print to the error stream. This is why I redirect its output to /dev/null.

like image 138
pathfinderelite Avatar answered Sep 21 '22 06:09

pathfinderelite