In this short article, we’ll talk about the different ways to compare branches: you can compare commits, actual changes, or even a specific file on two branches.
git diff can show you the difference between two commits:
git diff mybranch master -- myfile.cs
git diff mybranch..master -- myfile.cs
Note you must specify the relative path to the file. So if the file were in the src directory, you’d say
src/myfile.cs instead of
Using the latter syntax, if either side is
HEAD it may be omitted (e.g.,
You may also be interested in
git diff documentation):
This form is to view the changes on the branch containing and up to the second
<commit>, starting at a common ancestor of both
git diff A...Bis equivalent to
git diff $(git-merge-base A B) B.
In other words, this will give a diff of changes in
master since it diverged from
mybranch (but without new changes since then in
In all cases, the
-- separator before the file name indicates the end of command line flags. This is optional unless Git will get confused if the argument refers to a commit or a file, but including it is not a bad habit to get into.
The same arguments can be passed to
git difftool if you have one configured.
How to compare files from two different branches in git?
There are many ways to compare files from two different branches:
- Option 1: If you want to compare the file from n specific branch to another specific branch:
git diff branch1name branch2name path/to/file
git diff mybranch/myfile.cs mysecondbranch/myfile.cs
In this example, you are comparing the file in “mybranch” branch to the file in the “mysecondbranch” branch.
- Option 2: Simple way:
git diff branch1:file branch2:file
git diff mybranch:myfile.cs mysecondbranch:myfile.cs
This example is similar to the option 1.
- Option 3: If you want to compare your current working directory to some branch:
git diff ..someBranch path/to/file
git diff ..master myfile.cs
In this example, you are comparing the file from your actual branch to the file in the master branch.
More modern syntax:
git diff ..master path/to/file
The double-dot prefix means “from the current working directory to”. You can also say:
master.., i.e. the reverse of above. This is the same as
mybranch..master, explicitly referencing a state other than the current working tree.
v2.0.1..master, i.e., referencing a tag.
[refspec]..[refspec], basically anything identifiable as a code state to Git.
If you want to make a diff against the current branch you can commit it and use:
git diff $BRANCH -- path/to/file
This way it will diff from the current branch to the referenced branch (
I simply do
git diff branch1 branch2 path/to/file
This checks for differences between the files. Changes in
branch1 would be in red. Changes in
branch2 would be in green.
It’s assumed that
branch1 is the past and
branch2 is the future. You can reverse this by reversing the order of the branches in the diff:
git diff branch2 branch1
For Visual Studio Code I strongly suggest the extension:
Git History Diff
You can use it two compare files or even branches!
From the console, you can simply use this command :
git diff <Your_Branch> <Branch_To_Compare_With> -- myfile.cs
How to compare files from two different branches?
There are two scenarios to compare files:
Scenario 1: Compare files at remote branches (both branches should exists in the remote repository)
Scenario 2: Compare local files (at the local working area copy) to the files at the remote repository.
The logic is simple. If you provide two branch names to diff, it will always compare the remote branches, and if you provide only one branch name, it will always compare your local working copy with the remote repository (the one you provided). You can use range to provide remote repositories.
E.g., check out a branch:
git checkout branch1
git diff branch2 [filename]
In this case, if you provide filename, it will compare your local copy of filename with the remote branch named “branch2“.
git diff branch1 branch2 [filename]
In this case, it will compare filename from remote branches named “branch1” vs “branch2“
git diff ..branch2 [filename]
In this case also, it will compare filename from remote branches named “branch1” vs “branch2“. So, it’s the same as above. However, if you have just created a branch from another branch, say “master” and your current branch doesn’t exists on the remote repository, it will compare remote “master” vs. remote “branch2“.
There is another interesting point about these various ways of doing the comparison: I want to compare a file in my current branch to the same file in another branch. If I use
git difftool otherbranch.. filespec
I end up comparing two files which are actually in my temporary folder. However, If I use
git difftool otherbranch filespec
I end up comparing a file in my temporary folder (the version on otherbranch) with the actual file in my Git folder, which a) makes it much easier to tell which is which, and b) means I can use the diff tool (Beyond Compare 4 in my case) to copy changes from my other branch into my current branch.
When working with Git, it is quite common to use different branches in order to have work clearly separated from the main codebase.
However, when working on those branches, you might want to merge branches in order to have the resulting work in your main branch.
Before merging, you already know that you have to compare the differences between the two branches.
Comparing two branches is very beneficial : it can be used as a quick way to see if you will have merging conflicts.
It can also be commonly used in order to see the work that has been done : by comparing a feature branch with an integration branch for example.
In this tutorial, we are going to see how you can compare two Git branches easily.
Compare two branches using git diff
In order to compare two branches easily, you have to use the “git diff” command and provide the branch names separated by dots.
$ git diff branch1..branch2
Using this command, Git will compare the tip of both branches (also called the HEAD) and display a “diff” recap that you can use to see modifications.
In short, it will show you all the commits that “branch2” has that are not in “branch1”.
Let’s say for example that you are looking to see the differences between a feature branch (being one commit ahead of master) and the master branch.
In order to see what has been modified between master and feature, you would run the following command.
$ git diff master..feature diff --git a/file-feature b/file-feature new file mode 100644 index 0000000..add9a1c --- /dev/null +++ b/file-feature @@ -0,0 +1 @@ +this is a feature file
As you can see, one file has been added to the branch.
Git is using a color code in order to display differences done between two branches : lines in green are lines added to the files and lines in red are the ones that are deleted from the files.
Comparing two branches using triple dot syntax
In order to compare two branches, you can also use the “git diff” command and provide the branch names separated by three dots.
$ git diff branch1...branch2
So what’s the difference with the previous command?
Using “git diff” with three dots compares the top of the right branch (the HEAD) with the common ancestor of the two branches.
As always, a diagram speaks a hundred words, so here is the description of the diff command with three dots.
So which method should you use in order to compare two branches?
Most of the time, you want to stick with the first method, meaning using only two dots in order to compare two branches.
When you are developing a new feature, you are most of the time doing it on your own branch. However, developing on your own branch does not prevent the branch you checked out from to have other commits.
This is particularly true whenever you are checking out a new branch from the master branch : other commits might be integrated to master while you are working on your feature.
As a consequence, in order to compare two branches, you almost always want to stick with the first method we described.
$ git diff branch1..branch2
Compare commits between two branches
In some cases, you may be interested in knowing the commit differences between two branches.
In order to see the commit differences between two branches, use the “git log” command and specify the branches that you want to compare.
$ git log branch1..branch2
Note that this command won’t show you the actual file differences between the two branches but only the commits.
Back to the example we provided before, comparing the commit differences between the master and the feature branch would be written
$ git log master..feature commit 802a2abed7f88d67e0ab9a0e780b858651c5813b (HEAD -> feature, origin/feature) Author: SCHKN <firstname.lastname@example.org> Date: Wed Dec 4 13:10:01 2019 -0500 feature commit
If you are not interested in all the information provided by this command, there is a way to get shorter commit lines.
In order to compare two branches using commit abbreviations, use the “git log” command with the following options.
$ git log --oneline --graph --decorate --abbrev-commit branch1..branch2
Using the example we provided before, this command would give us the following output
$ git log --oneline --graph --decorate --abbrev-commit master..feature * 802a2ab (HEAD -> feature, origin/feature) feature commit
Compare specific file between two branches
In some cases, you may want to see all changes done to a specific file on the current branch you are working on.
In order to see the differences done to a file between two branches, use the “git diff” command, specify the two branches and the filename.
$ git diff master..feature -- <file>
Let’s say for example that the file that you modified between those two branches is called “README”.
In order to see the differences done to this file, you would run the following command
$ git diff master..feature -- README diff --git a/README b/README new file mode 100644 index 0000000..add9a1c --- /dev/null +++ b/README @@ -0,0 +1 @@ +this is the README file
Note that you can use the triple dot syntax we saw earlier in order to compare those files.
$ git diff master...feature -- <file>
Compare two branches using Sourcetree
In some cases, you might be interested in viewing differences in a Git graphical client.
For this example, I am going to use the popular Sourcetree Git GUI in order to display differences between two branches.
Given the repository view, you have access to all your branches in the left side menu.
In order to see the differences between two branches, on the Sourcetree left menu, click on the branch that you want to compare and click “Diff Against Current”
After clicking on “Diff Against Current”, you will be presented with the list of differences between your files, whether they are in your working tree or if they are in your index already.
In this tutorial, you learned how you can compare two branches easily using Git commands (specifically the git diff and git log commands).
You also learned that it is possible to use graphical tools such as Sourcetree in order to compare your branches and commits easily.
Hope you learned something from this post.
Follow Programming Articles for more!