2

This question is the same as the following question: How to do a git diff on moved/renamed file? but with the important distinction that I want to see the changes between the version of the file in the previous commit, and the version that is currently staged.

git diff --staged shows the whole file being deleted in one side:

--- a/old_path/main.cpp
+++ /dev/null
@@ -0,0 +1,42 @@
- line 1
- line 2
- line 3
- etc.

and the whole file being added on another:

--- /dev/null
+++ b/new_path/main.cpp
@@ -0,0 +1,42 @@
+ line 1
+ line 2
+ line 2.5
+ line 3
+ etc.

without any convenient way of comparing the actual differences between the two.

Community
  • 1
  • 1
augustin
  • 14,373
  • 13
  • 66
  • 79

2 Answers2

3

The answer is a bit different if you're looking at past commits, but the use of git diff --staged suggests you're looking for the difference between old_path/main.cpp in HEAD and your currently staged new_path/main.cpp, which was derived from the old file. In this case, this command should work, assuming that there have been no new changes to new_path/main.cpp since it was staged:

git diff HEAD:old_path/main.cpp new_path/main.cpp

Not sure how to specify exactly the version that is in the index for the second path in the case where new_path/main.cpp also contains additional changes that have not been staged yet...

Edit: after a little reading in gitrevisions(7) it would appear that this should handle the latter case:

git diff HEAD:old_path/main.cpp :new_path/main.cpp

Note the : with no revision prefix, which should specify the version that is in the index.

twalberg
  • 59,951
  • 11
  • 89
  • 84
  • +1, thanks. I wanted the second one, which works for my purposes (new_path/main.cpp contains both staged and unstaged changes). The first one gives what git diff --staged already gave. – augustin Nov 27 '12 at 07:53
  • man gitrevisions mentions "stage numbers (0 to 3)" but does not seem to define what there numbers correspond to... man git-stage/git-add appear to also be mute on the subject. Any idea? – augustin Nov 27 '12 at 07:59
  • Hmmm... maybe you need a newer version of the `git` man-pages... Mine (v1.7.12.3) says this under the `::` heading of `gitrevisions(7)`: "A missing stage number ... names a stage 0 entry. During a merge, stage 1 is the common ancestor, stage 2 is the target branch's version ..., and stage 3 is the version from the branch which is being merged." – twalberg Nov 27 '12 at 14:46
1

Since I don't know the answer to the question above, I usually use a roundabout way: whenever convenient, and especially whenever I remember, I make a file move change in a separate commit. Thus, once the move is committed, I can continue working on the file and can do a normal git diff --staged in the 'normal' way, preparing for the next commit. This is the easiest way that I know... the problem is, sometimes I forget to do it this way...

augustin
  • 14,373
  • 13
  • 66
  • 79