Git: How to compare different versions of a file

Updated: January 27, 2024 By: Guest Contributor Post a comment

Introduction

Git is a powerful version control system that is widely used among developers to manage and track changes in source code during software development. One of the key features of Git is the ability to compare different versions of files to see what has changed over time. This tutorial will guide you through the basics of comparing file versions in Git, and then progress to more advanced examples.

The Fundamentals

Before diving into file comparisons, you need to ensure that you have Git installed on your system and a Git repository at your disposal. If you are new to Git, you might first want to familiarize yourself with basic git commands like git clone, git add, git commit, and git status.

Once you’re set up, you can start comparing file versions using the git diff command.

Basic File Comparison

To compare the current state of a file with the las committed version use:

git diff filename.ext

This command shows the differences between the working directory and the last commit by default.

Comparing Specific Versions

Sometimes, you may need to compare the file as it was in different commits:

git diff commit1Hash commit2Hash filename.ext

Replace commit1Hash and commit2Hash with the respective commit hashes you want to compare.

Comparing Changes Over Time

To view how a file has changed over a few commits, you can use:

git diff commitHash..HEAD filename.ext

The above command compares changes from a specific commit to your current HEAD.

Comparing with a Different Branch

To compare a file between different branches:

git diff branchName filename.ext

This will show differences between the file on the current branch and the same file on another branch.

More Advanced Comparisons

Viewing Only Filenames

If you’re only interested in which files have changed, you can modify the diff command to show only filenames:

git diff --name-only branch1..branch2

Comparing Staged Changes

To see what changes you’ve staged that are ready to commit:

git diff --cached

Filtering Diff Output

You might want to filter the changes to only those that meet certain search criteria. You can use the -G option to search for differences that match a regular expression:

git diff -G'regex'

Diff with Line Numbers

To include line numbers in your diff output, which is not a default capability of Git Diff, you can use external tools such as diff-so-fancy or scripts to enhance the output:

git diff filename.ext | diff-so-fancy

Side-by-side Diff

For those who prefer to see diffs side by side, you can use

git difftool -y -t vimdiff

This command opens the difftool configured with Git to show you a side-by-side comparison.

Using External Tools

For an even more robust comparison, many developers use tools like GitKraken, SourceTree, or the built-in comparison tools in IDEs like Visual Studio Code, or IntelliJ IDEA. These tools often come with GUI (Graphical User Interface) which makes the comparison easier and more visual:

git difftool filename.ext

This command opens the default GUI difftool for a visual comparison.

Conclusion

Comparing different versions of a file in Git is an essential skill for developers to track changes and collaborate effectively. Whether you work with command-line tools or prefer GUIs, Git provides the flexibility you need. The commands covered in this tutorial represent the core techniques needed for efficient version comparison but remember—there’s always more to learn in Git’s rich feature set.