2

I have the following command:

git diff branch1..branch2 | grep '^diff'

...that will output files that contains differences between two branches. Sample output is like this:

diff --git a/path-to-file1/file1.php b/path-to-file1/file1.php
diff --git a/path-to-file2/file2.php b/path-to-file2/file2.php
diff --git a/path-to-file3/file3.php b/path-to-file3/file3.php

How do I amend this command to get only the file path? I want to filter out the "diff --git a/" and "b/path-to-file1/file1.php". So, I want the final output to look like this:

path-to-file1/file1.php
path-to-file2/file2.php
path-to-file3/file3.php

Assume both branches have the same directory structure. So take either the "a/" or "b/" paths.

1 Answer 1

8

That's a very backwards way to get files with differences. There's the --name-only option to git diff, which already does that, robustly.

How I found out? I had a similar problem as you when I was new to git, so I read git help diff.

4
  • YOU ROCK!!! Thank you!
    – MrSnrub
    Commented Nov 4, 2024 at 13:10
  • hahaha, thanks :) Hope this helped :) Commented Nov 4, 2024 at 13:12
  • 1
    when everything else fail, read the (fine) manual ?
    – Archemar
    Commented Nov 4, 2024 at 14:46
  • Me too! And after finding the option, it took me probably 3 years to stop typing --names-only rather than --name-only. Cause you know, most of the time, we expect multiple files and multiple names.
    – Kaz
    Commented Nov 22, 2024 at 2:14

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.