40

I have two branches in my repository which I want to diff for some files.

I want to list only newly added migrations between those two branches.

Something like:

git diff branch1 branch2 | grep /db/migrate

How can I do it?

3
  • 1
    What do you mean by "newly added migrations"? Commented Oct 21, 2016 at 9:11
  • 3
    Possible duplicate of Showing which files have changed between two revisions Commented Oct 21, 2016 at 9:11
  • newly added migrations between two branches one old and one new Commented Oct 21, 2016 at 9:13

2 Answers 2

55

This command will diff their whole history:

git diff branch1..branch2 --name-only

If you want to compare from their last common ancestor, then:

git diff branch1...branch2 --name-only

And now you can grep files that you want. From there it's easy to write a little shell script that diffs two branches, file by file.

 filenames=$(git diff branch1...branch2 --name-only | grep /db/migratons)
 IFS=' '
 read -r -a filearr <<< "$filenames"
 for filename in "${filearr[@]}"
 do
      echo $(git diff branch1...branch2 -- "$filename")
 done

Create the git-command-name file and put it into the user/bin folder (you should parametrize input - branches as variables).

Git will recognise it as a command that you can call with:

git command-name branch1 branch2
Sign up to request clarification or add additional context in comments.

4 Comments

Nice! I was looking for the --name-only option.
ditto on --name-only
I would change ", then:" to ", then (notice the extra period between the branch names) : "
--name-status also shows whether files are modified, added, deleted, renamed, etc.
3

It's even easier if you want to compare the current branch to another. While those familiar with git will think this obvious, I'm including this for people that are starting out with git.

git diff other_branch_name --name-only

1 Comment

The "obvious" part is that in git commands: if only 1 parameter is supplied, the current branch is assumed as the source (1st param), and the specified branch is the destination (2nd).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.