Git Diff: How to Compare Files Between Two Branches

The git diff command is a vital component of the Git toolkit, providing developers with a straightforward yet powerful means to compare and understand changes across branches.

The `git diff` command in Git plays a crucial role in comparing changes across Git data sources, like commits, branches, and files. One key use is comparing files in different branches, which is handy for managing parallel development or distinct project features.

In this article, we'll guide you through using git diff to compare files across branches in a Git repo.

Key Takeaways 

  • `git diff` is a vital tool in Git for comparing changes across branches, commits, and files.
  • `git diff` allows for selective file comparison by specifying the path to a particular file. .
  • Git offers additional options like `-b` for tailoring comparisons to specific needs.

Comparing Files Between Branches Using Git Diff 

When working on a project with multiple branches, developers often need to compare files to understand the changes made in each branch. This becomes especially important when merging or integrating changes from one branch into another. The git diff command provides a clear and concise way to view the discrepancies in file content between different branches.

How to Use Git Diff to Compare Files Between Branches

In this section, we'll walk you through the process of using git diff to compare files, offering insights into the changes between branches within a Git repository.

Move to the Desired Branch  

Before using git diff to compare files, ensure you are on the branch from which you want to initiate the comparison. Use this command to switch to the relevant branch:

git checkout <branch_name>

Replace "<branch_name>" with the name of the branch you want to compare files with. 

This step is crucial as the git diff command compares the current working directory (and the branch it represents) with a specified branch.

Use `git diff` to Compare Files

Now that you are on the desired branch, run the following command to compare a file in the current branch with one in another branch.

git diff <other_branch> -- <file_path>

Replace "<other_branch>" with the name of the branch you want to compare and "<file_path>" with the path to the specific file you want to analyze. Let’s use an example to demonstrate the usage.

Assume you have two branches - the master branch and the feature branch - both containing a file, fileA.txt. However, the two files have different data. The fileA.txt in the master has the following sentence: “This is a file in the master branch.” The fileA.txt in the feature has the following sentence: “This is a file in the feature branch.” 

Here is the git diff command to compare the two files from the master branch:

git diff feature-branch -- fileA.txt

This should give an output similar to this:

  • `diff --git a/fileA.txt b/fileA.txt`:
    •  Indicates that the comparison is for the file named fileA.txt.
  • `index 19262e2..70ee716 100644`
    • Represents the index or hash values of the file content in the source (19262e2) and target (70ee716) branches. 
    • `100644` indicates the file mode (normal file).
  • `--- a/fileA.txt`
    • Marks the beginning of the content for the original file (fileA.txt) in the source branch.
  • `+++ b/fileA.txt`
    • Marks the beginning of the content for the modified file (fileA.txt) in the target branch.
  • `@@ -1 +1 @@`
    • Indicates the line range being compared.
    • `-1` represents the line in the source branch, and `+1` represents the line in the target branch.
  • `-"This is file A in the feature branch."`
    • Represents the line that exists in the source branch (feature branch).
  • `+"This is file A in the master branch."`
    • Represents the line that exists in the target branch (master branch).

Additional Options for Enhanced Comparisons

Git offers additional options to tailor your comparisons to specific needs. These options not only refine the scope of analysis but also enhance your ability to discern changes more effectively. In this section, we’ll look at a few of them.

  • Ignore Whitespace Changes

To disregard changes in whitespace during file comparisons, utilize the `-b` or `--ignore-space-change` option with the git diff command. 

git diff -b <other_branch> -- <file_path>

This is particularly useful when whitespace modifications are not essential to your analysis.

  • Visual Diffs with difftool

For a more graphical representation of the differences, you can configure a difftool and use it to visualize the changes. 

Maintaining the previous example, we've chosen KDiff3 as our difftool to visualize the changes made to fileA.txt between the 'master' and 'feature-branch.' To do this, we run the following command: 

git difftool -t kdiff3 feature-branch -- fileA.txt

This command opens KDiff3 and displays a side-by-side visual comparison of the differences. In the screenshot below, you can see the interface of KDiff3 displaying the content of fileA.txt from the 'feature' branch on the left pane and from the 'master' branch on the right pane.

KDiff3 Interface
KDiff3 Interface

To learn more about git commands, check out our blog post: Unraveling the 6 Most Confusing GIT Commands 

FAQs

Q1. How do I check which branch I am currently on before using git diff?

Use the command git branch to display the current branch. The active branch is usually marked with an asterisk (*).

Q2. Can I use git diff to compare multiple files simultaneously?

Yes, you can compare multiple files by providing their paths after the branch names. For example:

git diff <other_branch> file1.txt file2.txt

Q3. Can I compare all files between two branches with git diff?

Yes, you can compare all files by omitting the <file_path> parameter. For example:

git diff <other_branch>

Q4. What does the index line in the git diff output represent?

The index line provides information about the commit IDs or SHA-1 hashes associated with the versions being compared. It helps identify specific versions of the file. 

Q5. What if there are conflicts during the merge?

If conflicts arise during the merge, Git will mark the conflicted areas in the affected files. Use `git status` to see which files have conflicts, and then resolve them manually before completing the merge.

Conclusion

In summary, the git diff command is a vital component of the Git toolkit, providing developers with a straightforward yet powerful means to compare and understand changes across branches. Its simplicity, combined with additional options like visual difftools, makes it an essential asset for managing code evolution and fostering effective collaboration in Git-driven projects. Mastering git diff enhances developers' ability to make informed decisions during parallel development and merges, contributing to the overall success of software development endeavors. 

Interested in learning Git with simple visualizations and animations as well as by solving lab challenges, check out our Git for Beginners Course.