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...
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With