Git : Determine if branch is in a merge conflict state

后端 未结 3 1251
臣服心动
臣服心动 2021-02-13 06:30

I am writing a bash script to do some automation. Part of the script involves navigating to a local repo, switching to the local master branch, then pulling the remote master to

相关标签:
3条回答
  • 2021-02-13 07:05

    Based on the answer given by torek, here is a ready-to-use snippet:

    CONFLICTS=$(git ls-files -u | wc -l)
    if [ "$CONFLICTS" -gt 0 ] ; then
       echo "There is a merge conflict. Aborting"
       git merge --abort
       exit 1
    fi
    
    0 讨论(0)
  • 2021-02-13 07:10

    Use git ls-files -u. It prints unmerged files. If it prints nothing, there are no unmerged files.

    However, while that's a direct answer for the question you asked "as asked", using git pull in a script is a bit dicey: pull is a convenience script that runs fetch for you, and then (depending on how you direct it and/or have configured your repo) runs either merge or rebase for you. Given that you are writing a script that has a particular goal in mind, you should most likely be using lower-level commands that do more-specific things. For instance, you might use git fetch followed by (as Etan Reisner suggested in a comment) git merge --ff-only so as to never attempt a merge.

    0 讨论(0)
  • 2021-02-13 07:11

    This lists files with a merge conflict:

    git diff --name-only --diff-filter=U
    
    0 讨论(0)
提交回复
热议问题