close
The Wayback Machine - https://web.archive.org/web/20241006234436/https://www.geeksforgeeks.org/git-diff/
Open In App

Git diff

Last Updated : 02 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Git is an essential tool for developers, enabling version control and collaborative work. One of its powerful features is the git diff command, which helps users understand changes made to files. In this article, we’ll explore git diff, breaking it down into simple terms and enhancing it with SEO-friendly content to help it reach a wider audience.

BERJAYA

What is Git Diff?

git diff is a command-line tool in Git that shows the differences between various states of a repository. It helps developers see what changes have been made, whether they are between working directory and the staging area, between the staging area and the last commit, or between any two commits.

Viewing Changes Between Commits

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.

BERJAYA

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.

BERJAYA

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.

BERJAYA

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.

BERJAYA

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

BERJAYA

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.

BERJAYA

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.

BERJAYA

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.

BERJAYA

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.

BERJAYA

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. 

BERJAYA

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/
BERJAYA

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.

BERJAYA

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.

BERJAYA

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
BERJAYA

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 
BERJAYA

Using command git diff commit-id-1 commit-id-2

To view difference with current branch

git diff name_of_branch
BERJAYA

Using git diff branch_name

To view the difference with commit_id

git diff commit id
BERJAYA

Using git diff commit_id 

To view the summary of changes

git diff --stat branch or we can write git diff -stat commit_id

BERJAYA

BERJAYA

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
BERJAYA

Using git diff –name-only commit-id command

To view files that are different than a branch

git diff --name-only branch_name
BERJAYA

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
BERJAYA

Using the command git diff –name-only commit-id folder-path



Similar Reads

How To Exit Git Log or Git Diff?
When working with Git, the git log and git diff commands are important for viewing commit history and changes between commits, respectively. However, if you're new to Git, you might find yourself stuck in these views, unsure how to exit. This article will guide you through the steps to exit git log or git diff, ensuring a smooth experience while na
2 min read
How To Ignore Whitespace With Git Diff?
Whitespace differences can clutter your Git diffs, making it harder to focus on substantial changes in your code. Fortunately, Git provides options to ignore these insignificant whitespace changes when comparing files. In this article, we’ll guide you through how to ignore whitespace with Git diff, enhancing your code review process and productivit
3 min read
How to Perform a Diff Between Two Stashes in Git?
Git stashing is a handy feature that allows developers to temporarily shelve changes without committing them. However, there might be scenarios where you need to compare the changes stored in different stashes. Git does not offer a built-in command to directly compare stashes, but there are several approaches you can take to achieve this. In this a
2 min read
Difference Between Git remote prune, Git prune and Git fetch --prune
Git is a version control system that helps developers manage and track changes in their codebase. Among its many commands, git remote prune, git prune, and git fetch --prune are essential for maintaining a clean and organized repository. This article will explain the differences between these commands, their syntax, uses, and provide examples to il
4 min read
PHP | Ds\Set diff() Function
The Ds\Set::diff() function is an inbuilt function in PHP which is used to create a set which contains the elements of the first set which are not present in the second set. Syntax: Ds\Set public Ds\Set::diff ( Ds\Set $set ) Parameters: It is used to hold the set, which value need to exclude. Return Value: It returns a new set containing the elemen
1 min read
PHP | Ds\Map diff() Function
The Ds\Map::diff() function is an inbuilt function in PHP which is used to create a map using the key which contains the elements of the first map which are not present in another map. Syntax: public Ds\Map::diff( $map ) Parameters: This function accepts a single parameter $map which is used to hold the map elements whose value need to be excluded.
2 min read
PHP | DateTime diff() Function
The DateTime::diff() function is an inbuilt function in PHP which is used to return the difference between two given DateTime objects. Syntax: Object oriented style: DateInterval DateTime::diff( DateTimeInterface $datetime2, bool $absolute = FALSE ) or DateInterval DateTimeImmutable::diff( DateTimeInterface $datetime2, bool $absolute = FALSE ) or D
2 min read
Collect.js | diff() Function
The diff() function compares the main collection against the given collection and returns the values which are in original collection but not in the given collection. In JavaScript, the array is first converted to a collection and then the function is applied to the collection. Syntax: data.diff(collection) Parameters: This function accept a single
2 min read
Moment.js moment.duration(x.diff(y)) Method
The moment().duration(x.diff(y)) method is used to create a duration using the difference between two Moment objects by using the diff() method. Syntax: moment().duration( x.diff(y) ); Parameters: This method accepts a single parameter that uses the diff() method to find the difference between two Moment objects. Return Value: This method returns a
1 min read
Moment.js moment().diff() Function
The moment().diff() function is used to get the difference in milliseconds of given dates which are passed as parameters to this function.  Syntax: moment().diff(Moment|String|Number|Date|Array, String, Boolean);Parameters: This function has two parameters, The first one is the date of type Moment|String|Number|Date|Array, and the second parameter
2 min read
NPM Diff Command
The npm diff command is used to compare changes between different versions of a package, changes in your working directory, or even changes in dependencies. This command helps developers identify modifications made to package files, dependencies, or configurations, allowing for a clear view of what has changed between two points. Table of Content I
4 min read
Git - git-show Command Line Utility
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
3 min read
Git LFS: Managing Large Files in Git Repositories
Git, undoubtedly one of the most popular version control systems, is widely used for managing source code in software development projects. However, one of its limitations is its handling of large files. Traditional Git repositories struggle to efficiently manage large files, leading to bloated repositories and slow performance. This is where Git L
4 min read
Git Subtree vs. Git Submodule
Git Subtree and Git Submodule are both mechanisms in Git that allow you to incorporate external repositories into your own repository. They provide a way to manage dependencies and include code from other projects while keeping your repository organized and maintainable. Git Subtree: Git Subtree allows you to insert a separate repository as a subdi
4 min read
How to Set Up Git Using Git Config?
Git is a powerful version control system that helps developers manage and track changes in their code. Setting up Git correctly is important for maintaining an organized and efficient workflow. In this article, we'll walk you through the process of setting up Git using the git config command. PrerequisitesBefore we dive into the setup process, make
3 min read
Difference between “git add -A” and “git add”
When working with Git, the git add command is used to move changes from your working directory to the staging area. However, there are different options and flags that can be used with git add, such as -A, which might lead to some confusion. In this article, we'll explore the differences between git add -A and git add. 1. Git add- CommandThe git ad
2 min read
Git - Difference Between Git Revert, Checkout and Reset
Git offers a range of commands to manage and manipulate your codebase. Among these commands, git revert, git checkout, and git reset are frequently used for different purposes. Understanding the differences between these commands is important for effective version control. In this article, we'll explore the functionalities and use cases of git reve
6 min read
How to Fix Git Error "Unable to create '/path/my_project/.git/index.lock'"?
The Git error "Unable to create '/path/my_project/.git/index.lock'" typically occurs when Git is unable to create or write to the index.lock file. This file is used to prevent simultaneous processes from modifying the repository, which could lead to corruption. Table of Content Remove the Lock File ManuallyUse Git Command to Remove the Lock FileRes
2 min read
Difference Between "git commit" and "git push"?
Git commit and git push are two essential commands you'll use a lot when working with Git. even their frequent use together, they have different functions. In order to help you understand when and how to use these two commands effectively in your version control workflow, this article will break down their differences. What is git commit?git commit
2 min read
Difference Between Git Fetch and Git Pull
Understanding the difference between git fetch and git pull is important for effective version control in Git. Git Fetch and Git Pull are two important commands in Git that help in managing remote repositories. While both commands involve retrieving data from remote repositories, they serve distinct purposes and are used in different contexts. In t
5 min read
Git - git prune
Git prune is used to remove the unreachable objects and clear up the local repository. Git is a powerful version control system that helps developers manage their code efficiently. However, as your project evolves, you may accumulate unnecessary data that clutters your repository. This is where git prune comes in view. Table of Content What is git
6 min read
Git For Non-Developers: How Different Industries Are Adopting Git?
While Git is widely recognized for its role in software development, it's increasingly being adopted by non-developers across various industries. This shift is driven by the growing need for version control, collaboration, and efficient project management in fields like marketing, design, data science, education, and even legal and finance sectors.
8 min read
Difference Between Git Push Origin and Git Push Origin Master
Understanding the difference between git push origin and git push origin master is important for efficient version control in Git. These commands are used to upload changes from your local repository to a remote repository, but they function differently. This article will explain these differences, how each command works, and when to use them. What
3 min read
Git vs. Other Version Control Systems: Why Git Stands Out?
Version control systems (VCS) are an essential tool for developers, enabling them to track changes, collaborate on projects, and manage codebases efficiently. Among the various VCS options available, Git has emerged as the dominant choice, used by millions of developers and organizations worldwide. While other systems like Subversion (SVN), Mercuri
8 min read
Git - Changing History
Git provides a very important option of altering the commit history. As the master of git, you can control and make it function according to you. Git gives you the authority to change the order of commits, change messages, remove commits entirely. Moreover, it also allows you to manage the project's history. However, it is advisable to commit the w
3 min read
Introduction to Git Branch
Branching means diverging from the mainline and continue to work separately without messing with the mainline. Nearly every VCS has some form of branch support. In Git, a branch is simply a reference to the commit, where the following commits will be attached. Git vs SVN: In recent years, the use of git has risen tremendously. Unlike SVN, git allow
3 min read
Git Features
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 which may b
9 min read
Bare Repositories in Git
Repositories in Git are a snapshot of the folder in which you are working on your project. You can track the progress and changes made to the project by making commits and also revert changes if not satisfactory. Repositories can be divided into two types based on the usage on a server. These are: Non-bare Repositories Bare Repositories What is a N
7 min read
Getting changes from a Git Repository
Git allows performing various operations on the Repositories including the local repositories and remote repositories. The user when downloads a project to work upon, a local repository is created to store a copy of the original project. This local repository stores the changes that are being made by the user. This way, the user can edit the change
4 min read
Git | Working with Stash
Pre-requisites: Git Git allows multiple users to work on the same project simultaneously. Suppose a developer is working on a feature in a branch and he needs to pull changes from some other developer's branch or if he has to work urgently on some other feature, but the feature he is currently working on is incomplete. In this case, you cannot comm
8 min read
Article Tags :