How to merge massive formatting-only commits between git branches

*** As always, first backup everything (your git working directory etc.) ***

Let’s assume formatting changed in branch A, and you’re working in branch B

To make life easier, prepare two git clones, the other one is checked out
with branch A (in dir gitA), the other one with branch B (in dir gitB).
You need to have both branches in gitB up-to-date, so:

gitB> checkout A
gitB> git pull
gitB> checkout B
gitB> git pull

Let’s assume the formatting-only megacommit has hashcode AAAA and the commit
before that has hashcode BBBB. You can check the latest commits to
branch A like this:

gitA> git log | less

First, merge A to B until the last commit before the formatting-only commit.

gitB> git merge BBBB

Do the usual merge-stuff and commit. Do not push. For later analysis store
the changed filenames:

gitB> git diff --name-only A > ../diffsbeforemerge

Close Eclipse or other clever software that is holding to your git working clone.

Clean:

gitB> git clean -d -x -f .

Copy all files to a safe place, e.g.

gitB> cd ..
xxxx> tar cf gitB.tar gitB

Create another tar file without .git directory:

xxxx> cp gitB.tar gitB-nogit.tar
xxxx> tar --delete -f gitB-nogit.tar gitB/.git

Merge the formatting-only megacommit (hashcode AAAA):

xxxx> cd gitB
gitB> git merge AAAA

Don’t worry about any conflicts… Just copy over the old files:

gitB> cd ..
xxxx> tar xf gitB-nogit.tar

At this point, format all source codes with the same formatter as in branch A,
e.g. by starting Eclipse and importing the correct formatter and then selecting
all projects and ‘Source/Format’.

Then:

xxxx> cd gitB
gitB> git add *
gitB> git commit -m "Synchronized formatting with branch A."

Now it is a good time to check what was changed before you push your changes
anywhere. Remember that you have the backup of the situation before merging,
so you can just wipe away the gitB directory and extract the tarball gitB.tar.

You can check that the formatting-only-merge did not add any new diffs to
branches:

gitB> git diff --name-only A > ../diffsaftermerge
gitB> diff -u ../diffsbeforemerge ../diffsaftermerge
Facebooktwitterredditpinterestlinkedinmail