1

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

You can also abbreviate HEAD as @, e.g., git diff @ B. And, you can use -R to reverse the direction of a diff, so you can write this as git diff -R B. (I would just write out HEAD, it's not that hard to type and it seems clearest :-) )

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.