Script to merge 2 git branches automatically?

北慕城南 提交于 2019-12-04 04:28:44

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.

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"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!