Cross-branch diff/patch in git

Posted: January 28, 2013 in git

Sometime it happens that you don’t have all your changes after merge/rebase.
I managed to solve this issue and just want to keep myself a reminder and share it.

So let’s imagine you have branch `development` and `myfeature`. Then you rebase changes from `myfeature` to `development`.
Even if you not get any conflicts, auto-merge could cause some of your changes to be wrongly merged.
To ensure all your changes are ok, you can make a diff between two branches as:

git checkout development
git diff --stat development..origin/myfeature

Note the `origin` portion. Two dots are also important. Until you pushed you only have your local branch copy changed and rebased, origin branch is still containing your full list of commits.
It will give you the differences.

We assume you have differences and you want to apply them on `development` branch. To do so you first need to create a patch.
Do make a patch you need to just run diff without any options (it produces patch by default) and save the output:

git diff development..origin/myfeature > myfeature.patch

Now you are ready to apply the missing code (this code is executed on current `development` branch). But let’s see what will change:

git-apply --stat myfeature.patch

This will show what changes are going to be applied. Now let’s test if all changes are ok to apply:

git-apply --check myfeature.patch

If it outputs nothing – you’re all set and can apply patch without issues. Let’s apply it:

git-apply myfeature.patch

Now you have patch applied and the missing code is in `development` branch!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s