bash Git:确定分支是否处于合并冲突状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27990830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git : Determine if branch is in a merge conflict state
提问by Jason McKindly
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 update the local master branch with the latest code.
我正在编写一个 bash 脚本来做一些自动化。部分脚本涉及导航到本地存储库,切换到本地 master 分支,然后拉动远程 master 以使用最新代码更新本地 master 分支。
Does anyone know if there's a way I can programatically determine if the pull resulted in a merge conflict so that I can bail at that point and not execute the rest of the script?
有谁知道是否有一种方法可以以编程方式确定拉动是否导致合并冲突,以便我可以在那时退出而不执行脚本的其余部分?
Any help / info is appreciated, thanks!
任何帮助/信息表示赞赏,谢谢!
回答by torek
Use git ls-files -u
. It prints unmerged files. If it prints nothing, there are no unmerged files.
使用git ls-files -u
. 它打印未合并的文件。如果它什么都不打印,则没有未合并的文件。
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 neverattempt a merge.
但是,虽然这是您“按要求”提出的问题的直接答案,但git pull
在脚本中使用有点冒险:pull
是一个fetch
为您运行的便利脚本,然后(取决于您如何指导它和/或已配置您的repo) 运行merge
或rebase
为您运行。鉴于您正在编写一个具有特定目标的脚本,您很可能应该使用执行更具体事情的较低级别的命令。例如,您可以使用git fetch
后跟(正如 Etan Reisner 在评论中建议的那样)git merge --ff-only
,以便永远不要尝试合并。
回答by flix
Based on the answer given by torek, here is a ready-to-use snippet:
根据 torek 给出的答案,这里是一个现成的片段:
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
回答by Michael Schmid
This lists files with a merge conflict:
这列出了具有合并冲突的文件:
git diff --name-only --diff-filter=U