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.

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.
Using git diffIllustration: 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-2Shows 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 --stagedUsing 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 commandHere 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 HEADWe 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 -vvHere 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_nameIt 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_nameTo 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_nameThe 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_nameHere 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_nameShowing 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^ HEADPatch-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.patchDifference 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_nameTo 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-2To view difference with current branch
git diff name_of_branch
Using git diff branch_nameTo 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 branchTo view files that changed after a certain commit
git diff --name-only commit-id
Using git diff --name-only commit-id commandTo view files that are different than a branch
git diff --name-only branch_name
Using git diff --name-only branch_nameTo 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
Similar Reads
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read
HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
Top 10 Projects For Beginners To Practice HTML and CSS Skills Learning to code is an exciting journey, especially when stepping into the world of programming with HTML and CSSâthe foundation of every website you see today. For most beginners, these two building blocks are the perfect starting point to explore the creative side of web development, designing vis
8 min read
Web Development Technologies Web development refers to building, creating, and maintaining websites. It includes aspects such as web design, web publishing, web programming, and database management. It is the creation of an application that works over the internet, i.e., websites.To better understand the foundation of web devel
7 min read