483

I am trying to customize the format for git log. I want all commits to be shown in one line. Each line should only show the first line of the commit message.
I found out that git log --pretty=short should do the trick but on my computer it shows the full log as git log does (besides the time stamp).

Further, I tried to use the placeholders as defined in the man page. Though, I could not find a command to shorten the log message. I tried this line git log --pretty=format:'%h : %s' which shows the shorted hash %hand the full message %s in one line.

I am using git version 1.7.3.1.msysgit.0 on Vista.


Maybe it has something to do with the way I write my commit messages. Here is an example:

Added some functionality.
+ Added print function in Foo class.
+ Added conversion from foo to baz.

So, with the example given I only want to be output Added some functionality. prepended by the shortend hash.

3
  • 1
    %s is the subject, not the full message.
    – Josh Lee
    Dec 18, 2010 at 17:47
  • It's really hard to tell what you actually want. The short format isn't all on one line, though (surprise!) oneline is. If oneline and %h : %s aren't what you want, what's wrong with them? In particular, %s is the subject, the first line of the commit message. That should indeed be a shortened version. Is it possible you've been making commits with a single long line for the message, and viewing them somehow with word wrapping?
    – Cascabel
    Dec 19, 2010 at 1:09
  • You totally understand. The problem is that oneline and %s do not output what we expect, at least on my computer. I updated my post with an example of my commit message. Maybe it helps.
    – JJD
    Dec 19, 2010 at 11:41

10 Answers 10

758

Have you tried this?

git log --oneline 

It's an alias for git log --pretty=oneline --abbrev-commit, and displays the "short sha" and "short description", for example:

9bee8857 Write more code
831fdd6e Write some code Second line of message

The problem is that you are missing an empty line after the first line of your commit message. The command above usually works for me, but I just tested on a commit without empty second line. I got the same result as you: the whole message on one line.

Empty second line is a standard in git commit messages. The behaviour you see was probably implemented on purpose.

The first line of a commit message is meant to be a short description. If you cannot make it in a single line you can use several, but git considers everything before the first empty line to be the "short description". oneline prints the whole short description, so all your 3 rows.

5
  • 1
    Thanks! You solved the mystery: I do not have an empty line after the first line in my commit message to separate the subject from the rest. Nevertheless, it would be nice if I would be free to put it in or leave it out.
    – JJD
    Dec 21, 2010 at 9:54
  • 1
    No problem. In fact, you are free to have the empty line or not. Only that your whole message becomes the short message if you leave it out. I like this short description / detailed description, and I suppose that being limited to one line for the short description was a problem, hence the empty line requirement. You could always pipe the output of git log to a filtering script, but I would really advocate writing a short description with empty line.
    – Gauthier
    Dec 21, 2010 at 10:27
  • 118
    There's also git log --oneline --oneline is a built-in shorthand for "--pretty=oneline --abbrev-commit" used together. Apr 23, 2013 at 21:26
  • how you get rid of the prepended shortened hashtag? Not that it isn't bit-packedly pretty. But without cut. That would be awkward... (BTW math Q: what's the odds the next bit in a properly encoded huffman sequence, at the storage level, is on or off? Hint: It's === 50%.)
    – Phlip
    May 19, 2014 at 3:14
  • 7
    that was it tx. Oh, also, I got a page full of s, so I naturally added a % for '%s'. C-;
    – Phlip
    Jul 17, 2014 at 18:15
213

Does git log --oneline do what you want?

2
  • 1
    I don't want the whole commit message to be merged onto one line. I want to see the first line of each commit message only.
    – JJD
    Dec 19, 2010 at 11:45
  • 1
    @JJD See my answer for that. Oct 5, 2020 at 7:50
121

If you need to print subject lines without commit hashes:

git log --pretty=format:%s

And as Mathieu mentioned, you can use %b for just the body and %B for both.

1
72

Better and easier git log by making an alias. Paste the code below to terminal just once for one session. Paste the code to zshrc or bash profile to make it persistant.

git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

Output

git lg

Output changed lines

git lg -p

Alternatively (recommended)
Paste this code to global .gitconfig file

[alias]
  lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

Further Reading.
https://coderwall.com/p/euwpig/a-better-git-log
Advanced Reading.
http://durdn.com/blog/2012/11/22/must-have-git-aliases-advanced-examples/

2
  • The example is a nice demo of pretty-format, but "Paste the code below to terminal just once" seems wrong because it does not create an alias for "git lg"
    – Drone2537
    Jul 3, 2017 at 17:40
  • 1
    note to others, if you put this command in batch, replace % with %%, replace ' with "". result=git log --color --graph --pretty=format:"%%Cred%%h%%Creset -%%C(yellow)%%d%%Creset %%s %%Cgreen(%%cr) %%C(bold blue)<%%an>%%Creset" --abbrev-commit %1 . You can pass -p to this bat file to get changed lines as well Mar 29, 2020 at 12:20
29

You can define a global alias so you can invoke a short log in a more comfortable way:

git config --global alias.slog "log --pretty=oneline --abbrev-commit"

Then you can call it using git slog (it even works with autocompletion if you have it enabled).

12

Without commit messages, only the hash:

git log --pretty=oneline | awk '{print $1}'
2
  • 8
    If you only want the hash... git log --pretty=format:"%H" Jan 9, 2017 at 14:47
  • If you only want the hash you can also use git rev-list HEAD. Jun 5, 2019 at 12:52
8

If you want to print commit-id and commit message only

git log --pretty=format:"%h %s"

%h is shorthand for hash_id and %s shorthand for subjectName[message_name]



You can show date behind commit-id and message by this command:

git log --pretty=format:"%h %s %C(yellow)(%cr)"

%C is a shorthand for color, I want to show date with different color for example (red,green,blue, yellow,...etc).

%cr is a shorthand for committer date, relative


Or you can use this command for print hash_id and the first word in the commit message:

git log --oneline | awk '{print $1 " " $2}'

Or

git log --oneline

6

if you only want the first line of the messages (the subject):

git log --pretty=format:"%s"

and if you want all the messages on this branch going back to master:

git log --pretty=format:"%s" master..HEAD

Last but not least, if you want to add little bullets for quick markdown release notes:

git log --pretty=format:"- %s" master..HEAD
1
  • 1
    Nice touch on adding the bullets before each line! Apr 16, 2021 at 10:25
4
git log --format="%H" -n 1 

Use the above command to get the commitid, hope this helps.

1
  • 1
    I used git log --format="%h %B" -1, and then in javascript, I split on \n and take the first line (which still works even if there is no newline) Sep 17, 2020 at 18:41
-10

if you want to always use git log in such way you could add git alias by

git config --global alias.log log --oneline

after that git log will print what normally would be printed by git log --oneline

1
  • 15
    This doesn't actually work, because (A. git aliases can't override built-in command names) and (B. There need to be quotes around "log --oneline").
    – Max Nanasy
    Mar 15, 2013 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.