I’ve been reaching more and more for git history commands to get details about the file I’m working on. I used to use tools like GitHub desktop or Sublime Merge but I never felt like they added that much value, it’s just faster to to call up a git log somefile
or git log -L 5,10:somefile
. The only shortcoming of this approach is it generally leaves me wanting a commit hash in my clipboard (often to switch to or to run git diff
with). No more! Today I doubled down grabbing these hashes without having to mouse over and select the hash; I give you:
git diff myfile --pretty=oneline | head -c7 | pbcopy
This is the most simple form of this that I can find.
--pretty=oneline
ensures the commit hash is first, piped into head -c7
we get the first 7 characters of the hash (you could grab more or use some kind of regex to get the whole thing but I believe 7 is the minimal amount you can give git where it will reliably find a commit). Pipe it to pbcopy
and you got a little git hash.
It’s a fair amount of typing, I think I could set --pretty=oneline
in my git config and frankly I could likely alias this whole thing as some kind of function in my .zshconfig but for now it is what it is.