Git diff
Git, a powerful version control system, offers a plethora of commands to manage changes to your codebase efficiently. Among these, git diff
stands out as a vital tool for understanding modifications between different versions of your files. In this article, we’ll see the intricacies of git diff
, exploring its functionality, various use cases, and tips for maximizing its utility.
What is Git Diff?
At its core, git diff
is a command-line utility that displays differences between different states of your Git repository. These states can be commits, branches, or the working directory. By comparing the content of files, git diff
helps you visualize changes, enabling you to understand modifications made over time.
So what git diff actually shows you all the changes done in the file after doing a commit for example:- a file say at.txt is modified here after doing a commit and here we can see that there is a difference in the file after a commit.
Illustration: Changes between two Commits as shown below as follows:
So now if you want to see the changes between the two commits use the following command: git diff commit-id1 commit-id 2 here we can see that in our first Commit Hello Beginner for Beginner ? is coming and in our second commit Hello Beginner for Beginner ? is coming here.
Shows difference for Staged files
So now if we want to see the changes between the previous commit and currently staged files we can use the following command:
git diff –staged. Also, there is one more command which is git diff –cached which we can use for the same use case. Also, we can use
git status -v which is just like a synonym for –staged one command.
Using git diff for comparing branches
For seeing the changes between different branches we will use the command git diff name_of _the_branch1 name_of_the_branch2. Now if we want to see all the changes on branch_2 for that we will use the command git diff branch1_name branch2_name.
Here we can see that when the command is git diff branch1_name brach2_name it is showing all the changes on the branch_name 2 that too in green color which means that those changes are staged and committed in that branch and in the second command which is just the reverse of the first command git diff branch2_name branch1_name it is showing all the changes in red color that means those changes are not tracked by
git in branch1 that is what the change in between the 2 branches. So now if I create a file in branch1 add it into the staging area and then commit it then those changes are getting tracked in branch1, not in branch2. So now if I use the command to show all the changes on branch 2 then it will show the change done in the master branch in red color and the change done in branch2 only will be in green color only. In a similar fashion, if we write git diff branch2_name branch1_name then it will show all the changes being done in branch1 in green color and changes being done in branch2 in red color.
Showing Both staged and unstaged Changes
For seeing all the staged and unstaged changes in git we use the following command: git diff HEAD
We can also use one more command for achieving this particular use case git status -vv it actually tells which changes are staged for commit and which are not.
Here all the changes which are under – sign are the changes that are not staged for commit.
Showing Differences for a Specific File or Directory
git diff file_name
It shows all the changes between the previous commit of the specified file and the locally-modified version that has not yet been staged. All the changes which are under the – sign are not staged.
It also works for directories and shows the changes between the previous commit of all files in the specified directory and the locally modified
versions of these files that have not been staged. Here all those changes which are coming in green color are the changes that are not staged.
To show the difference between some version of a file in a given commit and the local HEAD version you can specify the commit id of that commit and can compare it with the local head version you want. The local head version is basically the most recent change done in the file.
The change which is coming in the green color is the local head version that is the present state of the file which is the most recent change done in the file. Now if you want to see the version between two separate commits: git diff commit-id-1 commit-id-2 file_name.
Here we can see the version between two separate commits in green color for the respective commits ids. To show the difference between the version specified by the hash a1e7045 and the latest commit on the branch for the directory we can use this command:
git diff commit-id branch_name directory_name/
Showing differences between the current version and the last version
git diff HEAD^ HEAD
This command shows the changes between the previous commit and the current commit.
Patch-compatible diff: Sometimes we just need a diff to apply using a patch. So the command for that would be:
git diff --no-prefix > some_file.patch
This will create a patch_file because of this > symbol and that patch file will contain changes of the file such as changes that are staged and which are not staged. In general, it shows us the line changes the lines in green color are the changes that are recently made. So it works exactly the same just like the git diff.
Difference between two Commit or branch
To view the difference between the two branches we use the following command:
git diff branch1_name branch2_name
To view the difference between two commit id’s following command is used:
git diff commit-id-1 commit-id-2
To view difference with current branch
git diff name_of_branch
To view the difference with commit_id
git diff commit id
To view the summary of changes
git diff --stat branch or we can write git diff -stat commit_id
To view files that changed after a certain commit
git diff --name-only commit-id
To view files that are different than a branch
git diff --name-only branch_name
To view files in a folder that changed after a commit
git diff --name-only commit-id folder-path