Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare the working copy, staging copy and committed copy of a file using git

Tags:

git

git-diff

Assume for a moment I have a file in a repository called LICENSE.txt. The contents looks as follows:

Copyright 2014 MyCompany. All Rights Reserved.

Since its 2015 I changed the year so its 2015:

Copyright 2015 MyCompany. All Rights Reserved.

And then staged the file

git add LICENSE.txt

Being a tad distracted, I made another change to LICENSE.txt to reflect that another organization shares in the copyright.

Copyright 2015 MyCompany and Affiliates. All Rights Reserved.
Copyright 2015 Other Company. All Rights Reserved.

I'm able to see the difference between my working copy and staged copy by running

git diff 

And I'm able to see the difference between the staged copy and committed copy by running

git diff --cached

How do I compare the committed copy (the one without the year change) with working copy (the one with the additional copyright)?

This is purely an example. There are cases much more complex where I had the need to compare what I have staged with changes I subsequently made to the file. Comparing the contents of the working copy and the staged copy will determine whether I should replaced the staged copy or not.

I'm running git 1.9.5 on Windows Server 2012 R2.

like image 494
bloudraak Avatar asked May 04 '15 05:05

bloudraak


People also ask

Which command will show the differences between the staged and committed versions of the file?

The git diff command shows the differences between the files in two commits or between your current repository and a previous commit.

What should I type if I want to compare my current working copy to the copy that is in the staging area?

git diff - Compare working area to index. git diff --staged - Compare stage area to repository.

How do I compare codes in git?

You can run the git diff HEAD command to compare the both staged and unstaged changes with your last commit. You can also run the git diff <branch_name1> <branch_name2> command to compare the changes from the first branch with changes from the second branch.

How do I find the difference between two commits in git?

To see the changes between two commits, you can use git diff ID1.. ID2 , where ID1 and ID2 identify the two commits you're interested in, and the connector .. is a pair of dots. For example, git diff abc123.. def456 shows the differences between the commits abc123 and def456 , while git diff HEAD~1..


2 Answers

How do I compare the staged copy (the one with the year change) with working copy.
[...] I had the need to compare what I have staged with changes I subsequently made to the file

That would still be git diff.

(edit by OP: How do I compare the committed copy (the one with the year change) with working copy

Then it would be git diff HEAD.)

http://images.abizern.org.s3.amazonaws.com/365git/Feb11/Git%20Diff%202.png

(365git: Getting a diff between the working tree and other commits)

If you are looking for something different from git diff and diff --cached, that leaves you with:

git diff HEAD

That is: the difference between the version already committed and the working tree.

like image 165
VonC Avatar answered Sep 22 '22 17:09

VonC


  • git diff - Compare working area to index.
  • git diff --staged - Compare stage area to repository.
  • git diff HEAD - Compare working area to repository

To illustrate that, I changed a file with “Name Staged” text and then I added it (git add .). After that, I changed the file again, now I replaced the text to “Name Working Area” and then I run the following commands:

enter image description here

Now, you can see clearly how it works. Pretty cool, right?

like image 42
Pankwood Avatar answered Sep 22 '22 17:09

Pankwood