Get first 'x' characters of git log
Asked Answered
H

2

8

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

Hear answered 1/2, 2017 at 9:3 Comment(1)
Seems like a job for head.Feticide
C
0

Basically, it echoes multiple lines. so that your command cutting 40 characters from each line .

If you really want first 40 characters irrespective of line pattern, here is command

git log <branch_name> | awk '{print substr($0,1,40);exit}' 
Coprophagous answered 1/2, 2017 at 9:17 Comment(0)
P
13

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.

Popliteal answered 1/2, 2017 at 9:49 Comment(2)
The hash is all i need yes, but i figured since all im doing is checking if the master branch contains the commit it doesnt really matter if the word "commit" is part of the string. But thanks for sharing, knowledge is allways good :)Hear
There's a direct test for whether a branch contains a given commit, or more precisely, whether the commit that some name identifies has the given commit as one of its ancestors: git merge-base --is-ancestor. So if the question is: "is commit 1234567 merged into master" the direct shell script test is: if git merge-base --is-ancestor 1234567 master; then ...; else ...; fi. If the question is "which branch names are ancestors of master, use git branch --merged master. (In a sufficiently modern Git the --merged predicate is available in git for-each-ref as well.)Popliteal
C
0

Basically, it echoes multiple lines. so that your command cutting 40 characters from each line .

If you really want first 40 characters irrespective of line pattern, here is command

git log <branch_name> | awk '{print substr($0,1,40);exit}' 
Coprophagous answered 1/2, 2017 at 9:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.