3

How do I execute the following command getting filenames without paths?

So, literally, what the command says, "name-only" - not the file paths, just the file names.

git diff --name-only <commit> <commit>
3
  • you can always for i in `git diff --name-only <commit> <commit>` ; do basename $i ; done Commented Oct 9, 2014 at 13:58
  • What if you have two different README.txt files in different subdirectories? Why do you want to ignore the paths? Commented Oct 9, 2014 at 17:19
  • Then I'd like it to only show the README.txt file once. From my comment below, "My manager is content with the file paths included, I assumed he didn't want them, so I panicked and made this question" Commented Oct 13, 2014 at 16:43

1 Answer 1

3

AFAIK this is not provided by git since this is a pretty unusual requirement. For example my first try in my repository yielded about a dozen lines, each saying pom.xml because I just created a new version.


You can use a small script though. basename is your friend here.

I like xargs so I'd do it like this

git diff --name-only <commit> <commit> | xargs -n1 basename

This will fail if git diff provides no output.

Or for a loopy version

for s in `git diff --name-only <commit> <commit>` ; do basename $s ; done
Sign up to request clarification or add additional context in comments.

7 Comments

Didn't work: basename: missing operand. Also, I can't use bash.
@Everlight, if you can't use bash what can you use? Pretty much any shell will let you do something like this. Are you on Windows without basename? You should probably add any such constraints to your question.
I'm using RHEL 5.7. So, anything within the Linux Terminal.
@Everlight If you used the first version, this means there was no output from git diff. This version also has no particular dependencies on bash.
By the first version, I assume you mean your first suggestion, which gave me the output, "basename: missing operand <new line> Try `basename --help' for more information." which repeated for each line in the output. So, there was output. If it has no dependency on bash then I assume its my version of basename: 5.97 is the problem.
|

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.