chris
chris

Reputation: 344

How to get a particular line from git log --merges -n 1

When I try doing git log --merges -n 1 I see the below output

Merge: 12hjkda1222
Author: testuser
Date:   Tue Sep 1 22:38:42 2020 -0400

    test-1.1

    Merge in proj/repo from release/test-1.1 to master

    * commit 'aczlxhclzsjcfpod2369841':
      release

I just want the line which Merge in proj/repo from release/test-1.1 to master. Tried to use git log --oneline --format=%B -n 1 HEAD and git log --oneline --format=%B -n 1 HEAD | head -n 1 which doesn't output the results what I'm looking for. Is there a way that I can a particular line from the output

Upvotes: 1

Views: 314

Answers (1)

torek
torek

Reputation: 490178

This requires a three step process.

  1. Find the specific commit you care about. You can combine this with step 2 if git log is a usable method for finding the commit, but often the right way to find it is with a separate git rev-list command.

  2. Use git log with the --format directive, very similar to the way you are doing (just omit the --oneline which sets its own separate format). Use the %B format to get the message body, including the subject line, as you are doing.

  3. Pick the desired line, e.g., with sed or grep for a regular expression. That is, use the command from step 2 with a pipe into a second command that finds the line you like.

Since in this case, steps 1 and 2 can be combined easily, you can use, e.g.:

git log --merges -n 1 --format=%B | grep '^Merge '

or:

git log --merges -n 1 --format=%B | sed -n -e 3p

(note that the difference between these is that the grep command will find every line that matches the pattern, while the sed command simply prints the third line, regardless of which line, if any, starts with Merge ).

Upvotes: 3

Related Questions