375

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.

JJD
  • 44,755
  • 49
  • 183
  • 309
  • 1
    `%s` is the subject, not the full message. – Josh Lee Dec 18 '10 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 '10 at 01: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 '10 at 11:41

9 Answers9

617

Have you tried this?

git log --pretty=oneline --abbrev-commit

The problem is probably that you are missing an empty line after the first line. 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.

Gauthier
  • 34,041
  • 11
  • 53
  • 87
  • 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 '10 at 09: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 '10 at 10:27
  • 108
    There's also `git log --oneline` --oneline is a built-in shorthand for "--pretty=oneline --abbrev-commit" used together. – Jeremy Logan Apr 23 '13 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 `awk`ward... (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 '14 at 03: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 '14 at 18:15
193

Does git log --oneline do what you want?

9000
  • 37,110
  • 8
  • 58
  • 98
  • 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 '10 at 11:45
  • 1
    @JJD See [my answer](https://stackoverflow.com/a/51997006/3930247) for that. – technophyle Oct 05 '20 at 07:50
64

If you need no hashes and just the first lines (subject lines):

git log --pretty=format:%s
technophyle
  • 4,802
  • 2
  • 19
  • 41
48

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/

atilkan
  • 3,591
  • 1
  • 24
  • 34
  • 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 03 '17 at 17:40
  • 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)%%Creset" --abbrev-commit %1` . You can pass -p to this bat file to get changed lines as well – Dheeraj Bhaskar Mar 29 '20 at 12:20
  • 1
    Been using this forever! Thanks! – lacostenycoder Jan 15 '21 at 20:14
28

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).

That Brazilian Guy
  • 2,791
  • 4
  • 25
  • 46
10

Without commit messages, only the hash:

git log --pretty=oneline | awk '{print $1}'
JJD
  • 44,755
  • 49
  • 183
  • 309
otiai10
  • 3,149
  • 4
  • 32
  • 48
2

git log --format="%H" -n 1

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

  • 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) – Devin Rhode Sep 17 '20 at 18:41
2

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
gMale
  • 14,075
  • 17
  • 81
  • 109
-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

noisy
  • 5,701
  • 9
  • 47
  • 86
  • 13
    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 '13 at 20:43