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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Terraform Drift Detection at Scale: How to Catch Configuration Drift Early
  • Git Tags vs AWS Tags: A Tag-Tastic Showdown!
  • GitOps Software Development Principles – And the Benefits for the Entire Organization
  • Why GitOps Is Gaining Popularity in DevOps: A Deep Dive Into the Future of Infrastructure Management

Trending

  • GDPR Compliance With .NET: Securing Data the Right Way
  • A Guide to Developing Large Language Models Part 1: Pretraining
  • Hybrid Cloud vs Multi-Cloud: Choosing the Right Strategy for AI Scalability and Security
  • Beyond Linguistics: Real-Time Domain Event Mapping with WebSocket and Spring Boot
  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.

By 
Veera Sundar user avatar
Veera Sundar
·
Jun. 19, 11 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
271.6K 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.

Related

  • Terraform Drift Detection at Scale: How to Catch Configuration Drift Early
  • Git Tags vs AWS Tags: A Tag-Tastic Showdown!
  • GitOps Software Development Principles – And the Benefits for the Entire Organization
  • Why GitOps Is Gaining Popularity in DevOps: A Deep Dive Into the Future of Infrastructure Management

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!