Replacing one git branch with another one

2013-01-12 at 21:18:04 | categories: tips, git, linux

Git branch replace

One project I worked on had pretty long history in terms of number of commits and developers who came, did something and went. The result was that the master branch was not particular useful and the main project development concentrated on some other branch. Merging that branch back to master was close to impossible, since those 2 diverged 2 years before and were both pretty much changed. The decision was to replace the master with the other one. It was easily done using the -s ours switch of git merge:

git checkout new_master
# merge master - ignoring master's changes
git merge -s ours master
git checkout master
# finally merge all our stuff to new master - actually it replaces the master with new_master
git merge new_master
# now new_master can be deleted

It worked well and the whole history of master was preserved. Before that step some parts of the master (like new files) were manually moved to the new_master. Fortunatelly it was known what was added in the "old" master :-)