How to determine if Git merge is in process

后端 未结 5 1506
轻奢々
轻奢々 2020-12-09 09:30

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 pr

5条回答
  •  盖世英雄少女心
    2020-12-09 10:20

    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.

提交回复
热议问题