自动合并2个git分支的脚本?

发布于 2024-12-22 05:10:23 字数 652 浏览 0 评论 0原文

我的 git 存储库有 2 个分支:master 和development。我想要一个能够自动合并从开发到主控的所有更改的脚本。

我使用 Jenkins:Git 插件克隆存储库,然后运行此脚本(“版本”变量是作业参数):

# 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 -X他们的开发
冲突(删除/修改):在develop中删除test.txt并在HEAD中修改。 test.txt 的版本 HEAD 留在树中。
自动合并失败;修复冲突,然后提交结果。

如何自动解决此冲突?我希望脚本始终根据“开发”分支添加/修改/删除文件,因为无论如何都不会触及 master 。 。

My git repository has 2 branches: master and develop. I want a script that merges all changes from develop to master automatically.

I used Jenkins: The Git plugin clones the repository and then this script (the 'version' variable is a job parameter) is run:

# 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}

I tried it on a test repository and it fails with:

git merge -Xtheirs develop
CONFLICT (delete/modify): test.txt deleted in develop and modified in HEAD. Version HEAD of test.txt left in tree.
Automatic merge failed; fix conflicts and then commit the result.

How do I resolve this conflict automatically? I want the script to always add/modify/delete files according to the 'develop' branch, since master is never touched anyway...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花桑 2024-12-29 05:10:23

-X thoses 合并策略仅适用于解决文件中存在冲突的块。这些选项的文档位于 git-merge 手册中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.

在这种情况下,一个分支删除了该文件,而另一个分支修改了该文件,这与进行不同修改的两个分支之间的简单冲突块是不同的情况。

The -X theirs merge strategy only works to resolve conflicting hunks within a file. The documentation for these options is in 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.

In this case, one branch has deleted the file while the other has modified it, which is a distinct case from a simple conflicting hunk between two branches that have made different modifications.

一江春梦 2024-12-29 05:10:23

5 岁了……但仍然相关。

这是我的解决方案:
我删除主分支并从我想要“合并”的分支创建一个新的主分支:

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"

5 years old.... But still relevant.

Here's my solution:
I delete the master branch and create a new master branch from the branch I want to 'merge' from:

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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文