11

currently using:

git diff -name-status

Now this will only give me the following output:

README.md
created.txt
test.txt

However I am looking for something more like this:

/User/github/README.md
/User/github/created.txt
/User/github/test.txt

Does anyone know if something like this is possible?

To clarify: the comment from the linked post states: All output will return paths relative to git rev-parse --show-toplevel this is exactly the issue as I would want the path prior to the top level to be included.

2
  • Possible duplicate of Making git diff --stat show full file path Commented Feb 14, 2019 at 19:39
  • if you try to run the answers on that page the output is not what i am explaining. I am looking for system file paths outside of the git folders, and that answer only seems to explain file paths inside the git folder. Commented Feb 14, 2019 at 19:41

2 Answers 2

21

Building on torek's answer, I was able to achieve this in one line using the --line-prefix flag:

git diff --name-only --line-prefix=`git rev-parse --show-toplevel`/ HEAD release
Sign up to request clarification or add additional context in comments.

4 Comments

why not this? git diff --name-only --line-prefix="${PWD}/" HEAD master
The rev-parse answer worked well for me. PWD works if you are currently at the repo root.
@Gi0rgi0s PWD is the current directory of the shell and as such is much less reliable
While the accepted answer works, I believe this to be the best answer in the spirit of using the git tool to it's fullest.
6

Just add the top level yourself:

git rev-parse --show-toplevel

prints the path to the Git repository.

Feed the diff --name-only output through, e.g., sed -e "s,^,$path," (assuming no commas in your path name):

git-diff-with-abs-path() {
    local path

    path=$(git rev-parse --show-toplevel) &&
    git diff --name-only "$@" | sed "s,^,$path/,"
}

Edit #2: for --name-status, the name comes after a literal tab, so:

git-diff-with-abs-path() {
    local path tab=$'\t'

    path=$(git rev-parse --show-toplevel) &&
    git diff --name-status "$@" | sed "s,$tab,$tab$path/,"
}

Fancy this up a bit and you can make it pick --name-only or --name-status out of the $@ part and compute the correct sed expression, rather than hardcoding --name-only or --name-status.

2 Comments

One thing this does not do though is include the added modified deleted tag (which does show in name status) I guess i can append it to the back of the command somehow. but if there was a way to do something like: ' A /User/github/README.md M /User/github/Test.txt ' That would be more ideal
True, this is for --name-only. To handle --name-status, use a literal tab instead. I'll edit that into the answer.

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.