3

I'm using git diff on tags in a Bash window and its doing what I want for the most part.

$ git diff -- numstat TAG2 TAG1

Giving this output:

4  6  dir1/dir2/dir3/dir4/thisIsMyFile.cs

How can I get this output:

4  6  thisIsMyFile.cs

I don't need/want the path (dir1/dir2/dir3/dir4/), just the file name (thisIsMyFile.cs).

1
  • forgot to add that because of doing a diff on TAGs, there will be a number of files found that will have varying directories in the path. With all lines I still just want the file name. get this: 4 6 dir1/dir2/dir3/dir4/thisIsMyFile1.cs 22 12 dir1/dir2/thisIsMyFile2.cs 8 2 dir1/thisIsMyFile3.cs want this: 4 6 thisIsMyFile1.cs 22 12 thisIsMyFile2.cs 8 2 thisIsMyFile3.cs Commented Sep 21, 2017 at 13:59

3 Answers 3

2

As far as I know, there's no way to do this with Git alone (i.e., just giving Git diff the right arguments). You have to use a Bash pipeline to post-process the Git output. I suggest:

git diff --numstat TAG2 TAG1 | while IFS= read -r line; do
    printf '%s\t' "$(cut -f-2 <<<"$line")"
    basename "$(cut -f2- <<<"$line")"
done

This is a little more complicated because it is designed to preserve the original formatting and to parse correctly even if there are files with tabs or spaces in them. It could be simplified if you can guarantee no file has whitespace in its file name, or if you are interested in using a more powerful scripting language than Bash to parse the lines.

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

Comments

1

You can use awk:

git diff --numstat | awk -F'[/\t]' '{print $1,$2,$NF}'

What is the awk command doing?

-F'[/\t]' sets the field delimiter to either tab or /. (Output from git is tab delimited). $NF is the last field in the line - the filename.


Note: The above command will fail when the filename contains a tab. If you can live with that restriction, have fun!

Comments

0

use basename -a $(git diff --numstat TAG1 TAG2)

NOTE I'm using bash syntax to execute a sub-command.

Comments

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.