Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get first 'x' characters of git log

Tags:

git

git-log

cut

I'm trying to get only the first 40 characters of a git log, my current not working command is:

git log <branch_name> | cut -c 1-40 >> some_file

This outputs the whole log.

Kinda new to linux, any suggestions?

EDIT:

git log <branch_name> | head -n1 >> some_file

Working command per @Someprogrammerdude suggestion

like image 886
A.Jac Avatar asked Jan 04 '23 15:01

A.Jac


1 Answers

The | head method is fine—the head program is a general purpose filter for extracting the front part of an input stream, or some number of input files—but it's worth noting that the first line of the default git log output consists of the word commit followed by the commit's hash, which (perhaps not coincidentally) is spelled out as 40 characters:

$ git log | head -n 1
commit 8f60064c1f538f06e1c579cbd9840b86b10bcd3d

Since commit  (including the trailing space) is 8 characters long, if you chop this down to 40 characters, you get a 32-character abbreviation of the commit ID.

Since git log normally starts by showing you the HEAD commit, this all means that you are getting (part of) the hash ID of the HEAD commit, and there is a much more direct way to do that in Git:

$ git rev-parse HEAD
8f60064c1f538f06e1c579cbd9840b86b10bcd3d

This omits the word commit (and the space) but gets you the 40 characters that I suspect you care about. You can shorten the hash to any number of characters you like by adding --short or --short=count:

$ git rev-parse --short=12 HEAD
8f60064c1f53

In general, the way to turn a single name—such as master, or a tag name, or HEAD—into a Git object identifier (SHA-1 hash) is to use git rev-parse.

like image 135
torek Avatar answered Jan 12 '23 20:01

torek