Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git relies on the basis of distributed development of software where more than one developer may have access to the source code of a specific application and can modify changes to it that may be seen by other developers.
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.
Using git diff
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 Geeks for Geeks ? is coming and in our second commit Hello Geeks for Geeks ? is coming here.
Here can I use the command git diff commit-id-1 commitid-2
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 –staged
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.
Comparing the two branches by using the above command
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
Using 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.
Using git status -vv
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.
Using git diff file_name
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.
Using git diff folder_name
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.
Using git diff commit_id file_name
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.
Using the command git diff commit_id1 commit_id2 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/
Using the command git diff commit-id branch_name folder_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.
Using git diff HEAD^ HEAD
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.
Using git diff –no-prefix > file_name.patch
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
Using 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
Using command git diff commit-id-1 commit-id-2
To view difference with current branch
git diff name_of_branch
Using git diff branch_name
To view the difference with commit_id
git diff commit id
Using git diff commit_id
To view the summary of changes
git diff --stat branch or we can write git diff -stat commit_id
Using command git diff -stat branch to view the summary of changes in the branch
To view files that changed after a certain commit
git diff --name-only commit-id
Using git diff –name-only commit-id command
To view files that are different than a branch
git diff --name-only branch_name
Using 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
Using the command git diff –name-only commit-id folder-path
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!