Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nicely display file rename history in git log

Tags:

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.

like image 500
Jian Avatar asked Nov 19 '12 04:11

Jian


People also ask

How do I show changes in git log?

Find what file changed in a commit To find out which files changed in a given commit, use the git log --raw command. It's the fastest and simplest way to get insight into which files a commit affects.

How do I see file history in git?

Git file History provides information about the commit history associated with a file. To use it: Go to your project's Repository > Files. In the upper right corner, select History.

Which command is used to view the history of all the changes to a file?

Use --follow option in git log to view a file's history This lists out the commits of both the files.

Which command is used to view the history of the changes in git?

The git log command displays all of the commits in a repository's history.


1 Answers

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/%\n\n/ /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 (\n) using sed? which has the answer I based that bit on.

like image 122
FoolishSeth Avatar answered Jan 03 '23 00:01

FoolishSeth