Alexandre Santos
Alexandre Santos

Reputation: 8338

Git diff but only for changes on another branch

Suppose I have a branch0 in git.

From this branch0 I create a branch1 and start working on it. After a while I committed changes to this branch1. Meanwhile branch0 is being changed.

If I compare the latest version of branch1 against branch0 I will see all the changes between the branches, not only the changes that correlate to the changes in branch1.

For example, if a file was modified in the branch0, but the file was not modified on branch1, the git diff will show me the changes to the file. However the file was not modified on branch1. So I would like it to be ignored in the diff.

In a sense, I'd like to do a git diff between branch1 and branch0, but only for the files that were modified in branch1.

Is it possible in git? I can imagine doing it in bash, listing all the files and doing a diff on a one-by-one. But wanted to know if there's an easier way in git.

Upvotes: 7

Views: 2486

Answers (3)

Jason S
Jason S

Reputation: 1554

git diff branch0...branch1

will do what you want (note the 3 dots)

That is, it will show you only the changes made on branch1, since the common ancestor commit. The common ancestor commit, is the most recent commit made on both branches.

In the tree below, git diff branch0...branch1 will show you changes due to commits A, B, C. The common ancestor is E.

     A---B---C branch1
     /
D---E---F---G branch0

Upvotes: 4

jthill
jthill

Reputation: 60555

git diff between branch1 and branch0, but only for the files that were modified in branch1.

That's

git diff branch0 branch1 -- $(git diff --name-only branch0...branch1)

The three-dots form used above to generate the filenames is

git diff [--options] <commit>...<commit> [--] [<path>…]

changes on the branch containing and up to the second , starting at a common ancestor of both . "git diff A...B" is equivalent to "git diff $(git-merge-base A B) B". You can omit any one of , which has the same effect as using HEAD instead.

Upvotes: 8

Tim
Tim

Reputation: 43364

You can use compare arbitrary commits for that

Comparing with arbitrary commits

$ git diff test            <1>
$ git diff HEAD -- ./test  <2>
$ git diff HEAD^ HEAD      <3>
  1. Instead of using the tip of the current branch, compare with the tip of "test" branch.

  2. Instead of comparing with the tip of "test" branch, compare with the tip of the current branch, but limit the comparison to the file "test".

  3. Compare the version before the last commit and the last commit.

From examples at http://git-scm.com/docs/git-diff.

Example

Suppose you have this git tree

1---2 (branch0)
     \
      3 (branch 1)

where in commit 3 you have the changes made to branch1, now we apply and commit some changes to branch0

1---2---4 (branch0)
     \
      3 (branch 1)

If you now do

$ git checkout branch1
$ git diff branch0

it will show all the changes between these branches, including the changes that were applied in commit 4 like you already stated. But you only want to view the changes of branch1, which is basically a diff against the commit that it was branched off of, so this should do the job

$ git checkout branch1
$ git diff 2

where 2 is just an example of a commit hash and would actually look more like e542ghe or something similar

Upvotes: 1

Related Questions