Search code examples
gitgit-blame

Remove filename and timestamp from git-blame


Long filepaths and unneeded timestamps are making it hard for me to read the output of git blame. I'd like to remove these columns from the output, but keep the SHA, line number, and author name.

I see in the docs that the -f or --show-filename flags include the filename (even though it's included by default), but there doesn't appear to be a flag for removing the filename. I often use git blame on a single file, so I already know which file I'm looking at.

I also see a flag for suppressing both the author name and timestamp (-s), but I'd prefer to keep the author name, so this flag won't work for what I need.

I even tried to combine the -s flag with the -e flag (to use the author's email instead of their name), but that trick failed to output anything for the author.

Any ideas?


Solution

  • If there is no such option, one could use sed:

    git blame --date=short <filename> | sed -e 's, [^(]*, ,' -e 's, [^ ]*\( *[0-9]*)\), \1,'
    

    The first regex will need tiny adjustment if you have parentheses in the filenames. (hopefully you don't) It will work correctly even without filenames.