Difference Between Two Commits in Git
Git is the most demanding version control system in today’s era. We sometimes face a situation when we need to compare some data in our repository with some other data source available in another repository, and here we are to discuss one of its examples of how to compare these repositories.
This article will compare two different commits through the git diff
command in Git.
the git diff
Command
The command git diff
is used to reveal changes between the index and/or a working tree, changes between two different types of trees, changes that occur as a result of merging, changes that occur between two blob objects, or might be changes between two different files on the same disk. We can also see what text has been included, eliminated, and replaced in a file.
The syntax of the git diff
command is displayed below.
git diff
The above command shows any uncommitted changes to our current repository, which works as a default option.
git diff
Between Commits
Git has a wide range of commands for every situation we face daily. To examine the difference between the two commits, the command that Git offers is git diff
, which activates the diffing
function in Git.
This function is performed by taking two variables as an input and then presenting the changes between them; the variables are the commits of a file in their respective branches.
Here we will compare the files between two Git commits and specify the ref
’s name that refers to the commits we want to compare. Here, a ref
may be considered a commit ID
or a HEAD
, directly referring to the recent branch.
The syntax for the situation which is discussed above is below.
git diff <commit1> <commit2>
We will provide the commit hashes to see the difference between the two commits. The commit hash can be a complete SHA-1
hash, a little SHA-1
hash, or an ancestry path.
For instance, we are comparing two commits in our Git repository. To recover the ID
of the commits whose files we want to compare, we will execute the command git log –pretty=oneline
; it will give a concise result of all the commits in a repo
we have currently.
git log --pretty=oneline
The result of this command will be:
5141eaasasw323asassa2408bfcaassasa2323240 (HEAD ->; master) feat: Update README.md
3405340easagsdsaasa3232232395f1c2e docs: Create README.md
Let’s suppose we are going to compare these two commits. We will execute the following command.
git diff 5141ea9c41qwqwqwqw3232saas3322323223a910f2405240 3405340ee99df2aa6f5a23aswwqwqqwqwqw95f1c2e
As we have seen, the diffing
will be performed through the above command across our two commits, which we want to compare, and the results will be shown what is different in these two files.
Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.
LinkedInRelated Article - Git Diff
- How to Show Diff Details for Uncommitted Changes in Git
- How to Diff a File to an Arbitrary Version in Git
- How to Set Up Meld as Difftool and Mergetool for Git
- How to Set Up SourceGear DiffMerge Tool for Git
- How to Show Staged and Unstaged Changes in Git
- How to Compare Files, Commits, and Branches in Git