7

How can I print git commits to print only body (commit message without title) but in one line? So commit body lines are joined, possibly separated with space, and printed as one line for one commit.

For example, with two commits A and B, command:

$ git log --format=%b

prints:

Commit A, line A.1
Commit A, line A.2
Commit B, line B.1
Commit B, line B.2

But I'd like to get:

Commit A, line A.1 Commit A, line A.2
Commit B, line B.1 Commit B, line B.2
foka
  • 707
  • 6
  • 25

2 Answers2

6
git rev-list master |
    while read sha1; do
        git show -s --format='%B' $sha1 | tr -d '\n'; echo
    done

Let me explain:

git rev-list master

List SHA1 IDs of commits in the branch.

    while read sha1; do

Run a loop over every SHA1.

        git show -s --format='%B' $sha1

Show the body of the commit.

        tr -d '\n'

Remove all line endings.

        echo

Add one newline at the end.

Iulian Onofrei
  • 7,489
  • 8
  • 59
  • 96
phd
  • 57,284
  • 10
  • 68
  • 103
0

"3. By default, git log prints the commit, author's name and email ID, timestamp, and the commit message. However, the information isn't very graphical, especially if you want to see branches and merges. To display this information and limit some of the other data, you can use the following options with git log: $ git log --decorate --graph --oneline --all" ("Viewing the DAG, How to do it..." section of "Git Version Control Cookbook: Leverage version control to transform your development workflow and boost productivity, 2nd Edition"; by Aske Olsson, Rasmus Voss, Emanuele Zattin, Kenneth Geisshirt; publisher: Packt Publishing).

When sending emails to my boss, sometimes I needed to refer to the most recent commits or to a list of specific commits. I used to rely solely on git log -3 for example to display the last three commits. Unfortunately, that approach was verbose (each commit included multiple lines) and did not show the branch(es) that those commits belonged to. I started to use git log --decorate --graph --oneline --all, which allows me to show the branch(es) that each commit belongs to. Something I also like about this new approach is that each commit is summarized using a single line:

C:\Users\jaimemontoya\[path]\app>git log --decorate --graph --oneline --all
* 99d200c (HEAD -> improvedatesformat, origin/improvedatesformat) Subtract 4 hours to the date and time stored in the database because the database uses GMT but El Salvador and Guatemala use GMT-4.
* 244a7a9 Use date() and strtotime() to format date/time in an easy to read format without the verbose and inefficient approach of multiple switch case statements.
* 4d38145 Change date format to 5 June 2020 instead of 06/05/2020 to avoid ambiguity.
* 501d4e4 (markP/subscriptions, marksubscriptions) Change CAPTCHA to reCAPTCHA for contact us form.
* fc860b2 Add ability to send country-wide bulk emails using a template other than Default Template.
* 7f9d2e7 (origin/addsubscriptiontemplates, subscriptionbanneradministration, addsubscriptiontemplates) Remove code that supported template pictures uploaded to media directory, since that implementation was abandoned.
* f6ea277 Add models/subscription_template.php, the version that no longer contains the code that associates pictures to subscription templates.
* 4373e7a Merge branch 'marksubscriptions' into addsubscriptiontemplates

See it formatted with colors:

enter image description here

Jaime Montoya
  • 4,817
  • 4
  • 48
  • 75