Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
473 views
in Technique[技术] by (71.8m points)

nicely display file rename history in git log

The git command

git log --format='%H' --follow -- foo.txt

will give you the series of commits that touch foo.txt, following it across renames.

I'm wondering if there's a git log command that will also print the corresponding historical file name beside each commit.

It would be something like this, where we can interpret '%F' to be the (actually non-existent) placeholder for filename.

git log --format='%H %F' --follow -- foo.txt

I know this could be accomplished with

git log --format='%H' --follow --numstat -- foo.txt

but the output is not ideal since it requires some non-trivial parsing; each commit is strewn across multiple lines, and you'll still need to parse the file rename syntax ("bar.txt => foo.txt") to find what you're looking for.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can simplify it a little bit like this:

git log --format='%H' --name-only --follow -- README.md

which will give you output kind of like this

621175c4998dfda8da

README.md
d0d6ef0a3d22269b96

README.md

which should be a little easier to parse. For instance you can use a sentinel and sed out the newlines like this:

git log --format='%H%%' --name-only --follow -- README.md | sed ':a;N;$!ba;s/%

/ /g'

which should give you the hash and the filename on the same line:

621175c4998dfda8da README.md
d0d6ef0a3d22269b96 README.md

For info on the sed invocation, see How can I replace a newline ( ) using sed? which has the answer I based that bit on.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...