Let's imagine a situation when you have two branches in the Git repository. The first branch contains the old code. And the second one contains new code. Then, how would you compare those two branches?
Git already has the command that allows you to easily see the differences between two separate branches. Please review the following guide to learn how to compare a code in Git.
Table of Contents
- Specification of the git diff command
- Comparing two different branches
- Comparing two different commits
- Comparing changes in a project since the last commit
- Comparing commits or branches using the three dots syntax
- Compare current branch with another commit
- Compare a specific file only
- Compare a specific folder only
- Seeing the amount of changes between two branches or commits
- How to write output of the git diff command to a file
- Using the git log command
- Conclusion
Specification of the git diff command
You can use the git diff tool to compare the branches. It has to be executed in the terminal with the specific configuration.
It is worth noting that this command is complicated. There are many different options that can be passed into this tool. But in our guide, we will review the most common situations, like comparing commits, branches or files.
However, just in case, the following is a specification. You can see there how to use the git diff command.
git diff [<options>] <commit>..<commit> [--] [<path>...]
To save the space, the list of available options is not provided here. But if you'd like to review the detailed description of each option, you can run such a command in the Linux terminal.
man git diff
Comparing two different branches
It's very easy to compare two branches in Git. You have only to run the git diff command with the proper names of the branches to compare. Our example is given below.
git diff branch1 branch2
Also, you may notice in the official Git guide that there is also a two-dot syntax, like it's shown below.
git diff branch1..branch2
However, both those commands work exactly the same. You can use the option which is more comfortable for you.
Reducing the amount of output information
But default, the git diff tool shows exact information on what is different between various files. You can see changed lines and files. Sometimes it's very useful. But if the output is large, it may not be convenient to see which files were exactly changed.
However, the git diff utility has a special --name-only flag. If it's set, the actual diff information will not be printed to the Linux terminal. In other words, the console output will contain only the file names.
Following is an example of how to use this flag.
git diff --name-only branch1..branch2
But the above command can produce a simple list only. You will not see the status of each file. If you'd like to get extended information, you should use the --name-status flag, as shown below.
git diff --name-status branch1..branch2
If you run the given command, the console output will contain both file name and status of the file. You should see an additional letter in front of the file. Here is our example.
developer@developer-pc:~/projects/content$ git diff --name-status
M source/file1.js
M source/file2.js
M source/file3.js
M css/styles.css
M js/code.js
Important note: If you'd like to see a full list of statuses that are available in the Git, you may review the official documentation for the git diff command.
Comparing two different commits
Now, you know how to compare two branches. But what if you need to compare two different commits from the same branch? Git tool also can help you in such a case.
You have to use exactly the same command. But instead of branch names, you have to provide identifiers of the applicable commits.
The following example shows how to do it in the command line.
git diff ca7924eb3be07af9702da0bab2515d48c914282e 7d75a16800ef75cca25aab78407b7acc54d423f3
You may also use the two-dot syntax to compare the commits. As you have learned earlier, it works exactly the same as the regular syntax. See our example below.
git diff ca7924eb3be07af9702da0bab2515d48c914282e..7d75a16800ef75cca25aab78407b7acc54d423f3
If the output from the previous command is too large, you may add the --name-only flag to the git diff command. It allows you to see only the list of changed files. Following is an example of how to correctly use it.
git diff --name-only ca7924eb3be07af9702da0bab2515d48c914282e..7d75a16800ef75cca25aab78407b7acc54d423f3
In addition, you may print the status of each file to the console. It should be done with the help of the --name-status flag, as it's shown in our example.
git diff --name-status ca7924eb3be07af9702da0bab2515d48c914282e..7d75a16800ef75cca25aab78407b7acc54d423f3
Comparing changes in a project since the last commit
Sometimes, you may simply need to compare modified files in your current project. To do that, you have to execute the git diff tool without any parameters. It will show differences for the files that are not yet in a stage. See the example below.
git diff
Comparing commits or branches using the three dots syntax
Git has a special three-dot syntax, which works a little different than the two-dot syntax. It allows you to see what has changed since the last common commit. Specification is shown below.
git diff <commit>...<commit>
The real-life example may help you to understand how this method works. Let's imagine that the "feature" branch was created from the "main" branch some time ago, and they both share the same commit.
Now, if you run the git diff tool with three-dot syntax, the console output will contain changes that were added to the "main" branch since the creation of another branch.
git diff feature...main
Compare current branch with another commit
The Git version control system is very flexible. It's easy to compare a specific branch with another commit. To do that, you have to checkout the desired branch and then compare it with the commit. The following sample shows how to do it correctly.
git checkout main
git diff ca7924eb3be07af9702da0bab2515d48c914282e
Compare a specific file only
If you try to compare branches or commits in a very large project, the console output may be overloaded with various unneeded information. You would need to scroll the content and it may be hard to find the desired file on a screen. If your project is large and has thousands of files, then such operation may be very time consuming.
And to solve that issue, you may compare only the specific files. To do that, you have to add the "--" value to the command and then provide the name of the file to compare. See the following sample that shows the correct syntax.
git diff <commit>..<commit> -- CHANGELOG.txt
Compare a specific folder only
As you have seen in the previous section, it's easy to compare a single file. But the same way, you can easily compare a given folder.
That will also limit the output of the git diff command. You would need only to review specific content in the terminal. That can save you a lot of time if your project is big.
The syntax is the same, you just have to provide a path to the desired folder. See our example below.
git diff <commit>..<commit> -- ./folder/
Seeing the amount of changes between two branches or commits
Git tool also has a special "--stat" flag. It should be used if you'd like to see more details about what has changed. Please review the following sample that shows how to use that flag in the Linux terminal.
git diff --stat branch1..branch2
The same way, you can see the additional statistics when comparing two separate commits. You just have to replace branch names with the IDs of the applicable commits.
git diff --stat ca7924eb3be07af9702da0bab2515d48c914282e 7d75a16800ef75cca25aab78407b7acc54d423f3
How to write output of the git diff command to a file
Sometimes, you may want to save output of the git diff to a file on disk. That may save you time if you'd like to review the document later. In such a case, you can use the --output option. If it's set, the git diff tool will write the comparison results into the given file.
The following sample shows how to correctly use that option.
git diff branch1..branch2 --output=result.diff
However, there is an alternative method that allows writing content to the file. You can simply redirect output by using the ">" symbol. That's a standard functionality of any Linux operating system. Our example is given below.
git diff branch1..branch2 > result.diff
Using the git log command
You may also use the git log command to see differences between two branches. However, please pay attention that the output will be limited. You will see less information on the screen.
But, if you want to quickly see what has actually changed between branches, then this command may be helpful. Please see below how to correctly use it.
git log branch1..branch2
If you'd also like to see the commit history, you may use the special --graph flag. In that case, much more information will be printed to the screen. That usage is shown below.
git log --oneline --graph --decorate --abbrev-commit branch1..branch2
Conclusion
As you have seen in this guide, the Git version control system is pretty complex and has various flags and options. The learning curve is high. But still, you can easily achieve what you need, but that requires additional time for learning how exactly the Git tool works.
There are various methods that allow you to compare branches. You can get detailed information on what has changed in the repository. Also, you can limit the output of the git diff tool. For example, it's possible to see the history of changes for a specific file or folder.
Comments