579

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.

5
  • 1
    %s is the subject, not the full message. Commented 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? Commented 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. Commented Dec 19, 2010 at 11:41
  • Did I miss something or is there no answer to the original question yet? Commented Apr 24 at 16:47
  • I am using git log --oneline since a long time. I guess one part of the problem back then was that I forgot to insert an empty line between the commit summary and the commit body. Also, who remembers Vista at all :p Commented May 2 at 15:30

10 Answers 10

875

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
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.
There's also git log --oneline --oneline is a built-in shorthand for "--pretty=oneline --abbrev-commit" used together.
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%.)
that was it tx. Oh, also, I got a page full of s, so I naturally added a % for '%s'. C-;
232

Does git log --oneline do what you want?

2 Comments

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 See my answer for that.
162

If you need to print subject lines without commit hashes:

git log --pretty=format:%s

This will produce something like this:

Upgrade framework to v2
Add a new module
Hotfix for the production crash

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

1 Comment

%s for the subject; %b for the body; %B for both ("raw body" in git-scm.com/docs/pretty-formats)
89

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 Comments

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

Comments

13

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

Comments

11

Without commit messages, only the hash:

git log --pretty=oneline | awk '{print $1}'

2 Comments

If you only want the hash... git log --pretty=format:"%H"
If you only want the hash you can also use git rev-list HEAD.
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 Comment

Nice touch on adding the bullets before each line!
5
git log --format="%H" -n 1 

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

1 Comment

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

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 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.