我的git存储库有2个分支:master和develop。我想要一个脚本,自动将所有更改从开发合并到主。
我使用Jenkins:Git插件克隆存储库,然后运行此脚本('version'变量是作业参数):
# merge
git checkout -b develop origin/develop
git checkout master
git merge -Xtheirs --squash develop -m "v${version}"
# commit
git commit -m "v${version}"
# tag
git tag v${version} -m "v${version}"
# push
git push origin v${version}
我在测试存储库上尝试了它,它失败了:
git merge -Xtheirs develop
CONFLICT(删除/修改):在开发中删除test.txt并在HEAD中修改。 test.txt的版本HEAD留在树中。
自动合并失败;修复冲突,然后提交结果。
如何自动解决此冲突??我希望脚本始终根据'develop'分支添加/修改/删除文件,因为master永远不会被触及反正...
答案 0 :(得分:7)
-X theirs
合并策略仅适用于解决文件中存在冲突的问题。这些选项的文档位于the git-merge
man page:
ours
This option forces conflicting hunks to be auto-resolved
cleanly by favoring our version. Changes from the other tree
that do not conflict with our side are reflected to the merge
result.
This should not be confused with the ours merge strategy, which
does not even look at what the other tree contains at all. It
discards everything the other tree did, declaring our history
contains all that happened in it.
theirs
This is opposite of ours.
在这种情况下,一个分支删除了该文件,而另一个分支已经修改了它,这是两个分支之间进行了不同修改的简单冲突大块的明显情况。
答案 1 :(得分:2)
5岁....但仍然相关。
这是我的解决方案: 我删除了主分支,并从我想要的分支中创建了一个新的主分支' merge'从:
GIT_BRANCH_TO_MERGE_FROM=`git symbolic-ref HEAD | sed 's!refs\/heads\/!!'`
GIT_BRANCH_TO_MERGE_TO="master"
git checkout "${GIT_BRANCH_TO_MERGE_TO}"
git checkout "${GIT_BRANCH_TO_MERGE_FROM}"
# Delete TO branch
git branch -D "${GIT_BRANCH_TO_MERGE_TO}" || echo "Failed to delete ${GIT_BRANCH_TO_MERGE_TO}"
git push origin :"${GIT_BRANCH_TO_MERGE_TO}" || echo "Failed to push ${GIT_BRANCH_TO_MERGE_TO} delete to origin"
# Create TO branch
git checkout -b "${GIT_BRANCH_TO_MERGE_TO}" || echo "Failed to create local branch ${GIT_BRANCH_TO_MERGE_TO}"
git push origin "${GIT_BRANCH_TO_MERGE_TO}" || echo "Failed to push ${GIT_BRANCH_TO_MERGE_TO} to origin"