2

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?

Richie Thomas
  • 3,073
  • 4
  • 32
  • 55

1 Answers1

3

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.

niry
  • 3,238
  • 22
  • 34
  • 3
    You can get shorter than that, use `--date=short` on the blame to reduce the date to a single field, then your sed's `sed -e 's, [^(]*, ,' -e 's, [^ ]* \([0-9]*)\), \1,'` which strips any filename that doesn't have parens and the short date. – jthill Mar 28 '19 at 00:18
  • 1
    thanks @jthill, adjusted the answer per your comment. – niry Mar 28 '19 at 02:12
  • 1
    This works great. I wrapped it in a bash function named `gitb`, which I stashed in my .bash_profile. I replaced `` with the `$1` argument and now I can do things like `gitb Gemfile` and the output looks exactly how I want it to look. Cheers. – Richie Thomas Mar 28 '19 at 04:35
  • @RichieThomas I made a small fix is the regexp. The error was the variable number of spaces in before the line number broke removing the date. – niry Mar 29 '19 at 15:11