When merging topic branch \"B\" into \"A\" using git merge
, I get some conflicts. I know all the conflicts can be solved using the version in \"B\".
I a
I just recently needed to do this for two separate repositories that share a common history. I started with:
Org/repository1 master
Org/repository2 master
I wanted all the changes from repository2 master
to be applied to repository1 master
, accepting all changes that repository2 would make. In git's terms, this should be a strategy called -s theirs
BUT it does not exist. Be careful because -X theirs
is named like it would be what you want, but it is NOT the same (it even says so in the man page).
The way I solved this was to go to repository2
and make a new branch repo1-merge
. In that branch, I ran git pull git@gitlab.com:Org/repository1 -s ours
and it merges fine with no issues. I then push it to the remote.
Then I go back to repository1
and make a new branch repo2-merge
. In that branch, I run git pull git@gitlab.com:Org/repository2 repo1-merge
which will complete with issues.
Finally, you would either need to issue a merge request in repository1
to make it the new master, or just keep it as a branch.
Older versions of git allowed you to use the "theirs" merge strategy:
git pull --strategy=theirs remote_branch
But this has since been removed, as explained in this message by Junio Hamano (the Git maintainer). As noted in the link, instead you would do this:
git fetch origin
git reset --hard origin
Beware, though, that this is different than an actual merge. Your solution is probably the option you're really looking for.
I solved my problem using
git checkout -m old
git checkout -b new B
git merge -s ours old
This answer was given by Paul Pladijs. I just took his commands and made a git alias for convenience.
Edit your .gitconfig and add the following:
[alias]
mergetheirs = "!git merge -s ours \"$1\" && git branch temp_THEIRS && git reset --hard \"$1\" && git reset --soft temp_THEIRS && git commit --amend && git branch -D temp_THEIRS"
Then you can "git merge -s theirs A" by running:
git checkout B (optional, just making sure we're on branch B)
git mergetheirs A
I think what you actually want is:
git checkout -B mergeBranch branchB
git merge -s ours branchA
git checkout branchA
git merge mergeBranch
git branch -D mergeBranch
This seems clumsy, but it should work. The only think I really dislike about this solution is the git history will be confusing... But at least the history will be completely preserved and you won't need to do something special for deleted files.
A possible and tested solution for merging branchB into our checked-out branchA:
# in case branchA is not our current branch
git checkout branchA
# make merge commit but without conflicts!!
# the contents of 'ours' will be discarded later
git merge -s ours branchB
# make temporary branch to merged commit
git branch branchTEMP
# get contents of working tree and index to the one of branchB
git reset --hard branchB
# reset to our merged commit but
# keep contents of working tree and index
git reset --soft branchTEMP
# change the contents of the merged commit
# with the contents of branchB
git commit --amend
# get rid off our temporary branch
git branch -D branchTEMP
# verify that the merge commit contains only contents of branchB
git diff HEAD branchB
To automate it you can wrap it into a script using branchA and branchB as arguments.
This solution preserves the first and second parent of the merge commit, just as you would expect of git merge -s theirs branchB
.