Skip to content

Instantly share code, notes, and snippets.

@jooh
Created July 14, 2021 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jooh/ea73a83765bbcbc6d6e63e7113139d0e to your computer and use it in GitHub Desktop.
Save jooh/ea73a83765bbcbc6d6e63e7113139d0e to your computer and use it in GitHub Desktop.
Use git merge driver to always resolve conflicts in file VERSION in favor of merge target
Use git merge driver to always resolve conflicts in file VERSION in favor of merge target
VERSION merge=always-ours
[merge "always-ours"]
name = Merge Ours
driver = true
#!/bin/bash -x
# Demonstrate how to use a merge driver to ensure that a single file VERSION always gets
# resolved as --ours in a merge conflict
echo "REPO SETUP"
# start clean
rm -Rf .git .gitbackup
git init
echo "CHECK OUT THIS .gitconfig"
cat .gitconfig
echo "CHECK OUT THIS .gitattributes"
cat .gitattributes
echo "CHECK OUT HOW .git/config LOOKS AFTER DOING include.path ../.gitconfig"
echo "(it's ../ because it's relative to .git sub-dir)"
git config --local include.path ../.gitconfig
cat .git/config
echo "first" >| README
echo "first" >| VERSION
git add .gitconfig .gitattributes README VERSION
git commit -m "first commit"
echo "CHECK OUT BRANCH AND CHANGE STUFF"
git checkout -b featurebranch
echo "branch" >| README
echo "branch" >| VERSION
git commit -am "branch commit"
echo "CHECK OUT MASTER AND MAKE CONFLICTING CHANGES"
git checkout master
echo "master" >| README
echo "master" >| VERSION
git commit -am "conflicting master commit"
# now there's two ways things could go so let's back up the .git folder
cp -r .git .gitbackup
# if we merge from master, we want to nuke the featurebranch change to VERSION
echo "MERGE FEATUREBRANCH ON TO MASTER"
echo "EXPECTED OUTPUT:"
cat VERSION
git merge featurebranch
cat VERSION
# now test in the other direction
echo "MERGE FEATUREBRANCH ON TO MASTER"
git reset --hard HEAD
mv -f .gitbackup .git
git checkout featurebranch
echo "EXPECTED OUTPUT:"
cat VERSION
git merge master
cat VERSION
+ echo 'REPO SETUP'
REPO SETUP
+ rm -Rf .git .gitbackup
+ git init
Initialized empty Git repository in /Users/jc01/nesta/mergedriver_demo/.git/
+ echo 'CHECK OUT THIS .gitconfig'
CHECK OUT THIS .gitconfig
+ cat .gitconfig
[merge "always-ours"]
name = Merge Ours
driver = true
+ echo 'CHECK OUT THIS .gitattributes'
CHECK OUT THIS .gitattributes
+ cat .gitattributes
VERSION merge=always-ours
+ echo 'CHECK OUT HOW .git/config LOOKS AFTER DOING include.path ../.gitconfig'
CHECK OUT HOW .git/config LOOKS AFTER DOING include.path ../.gitconfig
+ echo '(it'\''s ../ because it'\''s relative to .git sub-dir)'
(it's ../ because it's relative to .git sub-dir)
+ git config --local include.path ../.gitconfig
+ cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[include]
path = ../.gitconfig
+ echo first
+ echo first
+ git add .gitconfig .gitattributes README VERSION
+ git commit -m 'first commit'
[master (root-commit) f052012] first commit
4 files changed, 6 insertions(+)
create mode 100644 .gitattributes
create mode 100644 .gitconfig
create mode 100644 README
create mode 100644 VERSION
+ echo 'CHECK OUT BRANCH AND CHANGE STUFF'
CHECK OUT BRANCH AND CHANGE STUFF
+ git checkout -b featurebranch
Switched to a new branch 'featurebranch'
+ echo branch
+ echo branch
+ git commit -am 'branch commit'
[featurebranch ae20e61] branch commit
2 files changed, 2 insertions(+), 2 deletions(-)
+ echo 'CHECK OUT MASTER AND MAKE CONFLICTING CHANGES'
CHECK OUT MASTER AND MAKE CONFLICTING CHANGES
+ git checkout master
Switched to branch 'master'
+ echo master
+ echo master
+ git commit -am 'conflicting master commit'
[master ce3aeb0] conflicting master commit
2 files changed, 2 insertions(+), 2 deletions(-)
+ cp -r .git .gitbackup
+ echo 'MERGE FEATUREBRANCH ON TO MASTER'
MERGE FEATUREBRANCH ON TO MASTER
+ echo 'EXPECTED OUTPUT:'
EXPECTED OUTPUT:
+ cat VERSION
master
+ git merge featurebranch
Auto-merging VERSION
Auto-merging README
CONFLICT (content): Merge conflict in README
Automatic merge failed; fix conflicts and then commit the result.
+ cat VERSION
master
+ echo 'MERGE FEATUREBRANCH ON TO MASTER'
MERGE FEATUREBRANCH ON TO MASTER
+ git reset --hard HEAD
HEAD is now at ce3aeb0 conflicting master commit
+ mv -f .gitbackup .git
+ git checkout featurebranch
Switched to branch 'featurebranch'
+ echo 'EXPECTED OUTPUT:'
EXPECTED OUTPUT:
+ cat VERSION
branch
+ git merge master
Auto-merging VERSION
Auto-merging README
CONFLICT (content): Merge conflict in README
Automatic merge failed; fix conflicts and then commit the result.
+ cat VERSION
branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment