Sling Academy
Home/DevOps/Git: How to compare different versions of a file

Git: How to compare different versions of a file

Last updated: January 27, 2024

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.

Next Article: Git: How to view working tree changes with ‘git diff’

Previous Article: 4 Ways to Solve Git Merge Conflicts

Series: Git & GitHub Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide