DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Git Tutorial: Comparing Files With diff

Git Tutorial: Comparing Files With diff

The most common scenario to use diff is to see what changes you made after your last commit. Let’s see how to do it.

Veera Sundar user avatar by
Veera Sundar
·
Jun. 19, 11 · Tutorial
Like (2)
Save
Tweet
Share
270.79K Views

Join the DZone community and get the full member experience.

Join For Free

Continuing from my last post, which explained my Git workflow, in this post I’m sharing how to compare different versions of files. Before that, you might want to read the Getting started with Git post too if you haven’t done already.

The main objective of version controlling is to enable you to work with different versions of files. Git provides a command diff to let you to compare different versions of your files.

The most common scenario to use diff is to see what changes you made after your last commit. Let’s see how to do it.

I opened the helloworld project from my last example with a clean working directory. i.e. I have already committed all my code changes. So, a git status will give an output like this:

C:\vraa\projects\helloworld> git status
# On branch master
nothing to commit (working directory clean)

Let’s make a change in the helloworld.txt file now and compare this file with previously committed version.

C:\vraa\projects\helloworld> edit .\helloworld.txt
C:\vraa\projects\helloworld> git diff HEAD .\helloworld.txt
diff --git a/helloworld.txt b/helloworld.txt
index e4f37c4..557db03 100644
--- a/helloworld.txt
+++ b/helloworld.txt
@@ -1 +1 @@
-Hello India
+Hello World

There it is. Git shows the exact change I made in the file. But, if you look at the diff command, you might wonder what HEAD is doing there! Well, it is there for a purpose.

If you can recall, Git has an index between local repository and your working directory. So most of Git commands can either refer to index or the local repo. When you say HEAD in your Git command, it refers the local repo.

git diff HEAD [filename] //  compare the working directory with local repository.
git diff [filename] // compare the working directory with index.
git diff --cached [filename] // compare the index with local repository.

You can also compare files between two different commits. Every commit in Git has a commit id which you can get when you give git log. Then you can use the commit id if diff command like this.

git diff 7eb2..e03 812...a3f35

You can compare not just a single file, but all your changes at once. If you made changes in many files, just don’t mention any file name in the diff command which will diff all the changed files.

git diff // compares working directory with index, i.e. shows the changes that are not staged yet.
git diff HEAD // compares working directory with local repository. shows the list of changes after your last commit.
git diff --cached // compares index with local repository. shows the diff between your last commit and changes to be committed next.

That’s it about the basic introduction to compare files in Git. If you have any comments about this series, do let me know. I’ll continue with other topics in my next post. 

From http://veerasundar.com/blog/2011/06/git-tutorial-comparing-files-with-diff/

Git Diff

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Apache Kafka Is NOT Real Real-Time Data Streaming!
  • Using GPT-3 in Our Applications
  • Building a RESTful API With AWS Lambda and Express
  • Introduction to Container Orchestration

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: