Dave Heq
Dave Heq

Reputation: 364

Strange git diff behavior

I'm on branch A and branch B is ahead of it. I want to see the changes in B from A, but when I run git diff B it shows its additions as deletions and vice-versa, and if I want to put B's changes into the working copy of A (desirable if I have a test offshoot branch), trying to run git diff B > Bfile then git apply Bfile just throws a bunch of errors.

If I instead type git diff A..B (I'm already on A) it shows the correct changes.

So why does the first command show the changes as deletions when they're additions? It seems redundant and counter-intuitive to have to type out the branch I'm already on when I just want the accurate changes with another branch.

Upvotes: 1

Views: 221

Answers (1)

Scott Weldon
Scott Weldon

Reputation: 10227

(Note that git diff A B is synonymous with git diff A..B,so I'll use both interchangeably in this answer.)

The behavior you described is because git diff B is equivalent to git diff B HEAD, which shows the changes from B to HEAD. Your workaround, git diff A..B is valid (as would be git diff HEAD..B). To skip the requirement to specify the A branch, (or HEAD), do:

git diff ..B

This will tell Git to fill in HEAD on the left instead, so it's equivalent to git diff HEAD..B.

Upvotes: 1

Related Questions