Git: Compare Differences Between Branches

The git diff command will allow you to view the differences in your workspace. This can be used in a number of ways to look at the differences in a file, in a branch or between two branches.

For example, you can view the differences in your current workspace by just running git diff on its own. By supplying a file you can look at the differences in just that file.

To compare the difference between one branch and another you use '..' to separate the branch names. For example, to look at the differences between the head of the 'main' branch and the head of a feature branch use this syntax.

git diff main..feature_branch

This can often be quite noisy, especially on large projects, so to check just one directory between two branches you can add the directory path to the command.

git diff main..feature_branch directory/to/compare

The output produced from this difference can be used as a patch file. If you need to do this then just pipe the output into a file and you have a patch file you can use to apply the changes made against the feature branch to the main branch.

git diff main..feature_branch > feature.patch

You can use the same syntax to look at the commits that are different between two branches. This will show the commits that have been made to one branch, but not to another.

git log main..feature_branch

This will just show the individual commits, so to show some context to those commits you can use some flags on the log command to dive the commits some structure.

git log --oneline --graph --decorate --abbrev-commit main..feature_branch

One thing to note is that the use of 'main..feature' will compare the head of one branch with the head of another branch. To show the difference between the head of a feature branch and where it was branched off from the parent you need to use the triple dot (...) to separate the two branches.

For example, this will show all of the changes made to the feature_branch branch since it was branched off of the main branch.

git diff main...feature_branch

Finally, if you are using either the double or single dot comparison then make sure you get the order of the items correct. You generally want to use the main/parent branch as first item as this will show you any removals and additions made to the feature branch in comparison to the parent branch. This means that if you create a patch file from this diff you will be applying the correct changes.

If you get the branches the other way around then you are looking at the inverse of the changes. Essentially, is the changes that need to be made to the feature branch in order to get it to look like the parent branch. If you use the double dot comparison create, a patch from this difference, and apply it to your feature branch code you will essentially reset that branch to be the same as the parent.

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
1 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.