245

How can I show the name of branches in the output of git log?

For example with, git log --graph --all I get a nice overview of the commits, but get confused which line is master, and which is my branch for example.

peterh
  • 11,875
  • 18
  • 85
  • 108
vdboor
  • 21,914
  • 12
  • 83
  • 96
  • 1
    Handily, showing the names of linked heads or tags appears to already be done by default since some recent `git` update. – underscore_d Aug 02 '17 at 10:29

5 Answers5

365

Try the decorate option.

git log --graph --all --decorate

It annotates commits which are pointed to by tags or branches.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • 45
    With `--pretty` you can use `%d` where you want the 'decorations'. – CB Bailey Dec 03 '09 at 17:41
  • 2
    I'm confused.. this does what I want for some commits (shows the branch where the commit was made) but for many commits, it doesn't show the branch name at all. Anyone know why that might be? – noli Mar 11 '13 at 19:28
  • 5
    @noli: What are you expecting it to show? Not all commits are branch tips. – CB Bailey Mar 11 '13 at 19:42
  • 2
    thanks for responding charles, maybe I'm confused about what you mean by "branch tips"? Right now, I always make a branch to do my work on, and sometimes I want to look through the log to see all commits that were done on a given branch (so I can see all of the related feature changes easily). I was expecting to see the branch name in the log for all commits, but I only see it in some of them – noli Mar 11 '13 at 19:52
  • @noli: Decorate will only 'decorate' commits which actually have a branch pointing at them. If you want to view commits on a branch, you just do `git log `. – CB Bailey Mar 11 '13 at 20:42
  • 2
    @CharlesBailey Actually displaying this info for each commit is exactly what i want too. Lets assume i have a common base for two branches. Usually i do some code review and i would like to see which commits are also on another branch without showing full history (Only the history of this branch) – mhstnsc Apr 08 '13 at 09:08
  • 1
    I like the command from http://stackoverflow.com/questions/9437182/git-show-all-branches-but-stashes-in-log it's like git shortlog but for graphing the branching pattern. – Ninjaxor Apr 17 '13 at 23:49
  • 12
    @noli git only stores the branch name at the most recent commit of the branch (the tip). All commits in the history are equal and anonymous. If you want named branches so that every commit carries the branch name, you can use Mercurial. – Sampo Smolander Jul 25 '13 at 07:11
  • @MiHai, @noli: Try `git log ^master otherbranch` to show commits that are on `otherbranch` and not on `master`. I came here looking for something like the output of `git branch --contains commitid` for each commit. Looks like that doesn't exist? – Wodin Jul 09 '14 at 07:08
  • 8
    @CharlesBailey: I like the colors of the refs with `git log --graph --all --oneline --decorate`. I have an alias `git graph` that uses `--pretty` (in order to show other stuff as well, such as author and date), but `%d` there does not give me the colors of `--decorate`. I use yellow for all my refs for now, do you know how I can let `--pretty`'s `%d` string inherit the colors of `--decorate`? – Gauthier Mar 18 '15 at 14:11
  • 6 years after - very usefull! – Colonel Beauvel May 27 '15 at 09:18
  • Great! I did this alias with it: `git config --global alias.l 'log --graph --all --decorate'` so now I just need to type `git l` to see it. – bitifet Jun 28 '17 at 10:47
  • What does "(HEAD -> feature2, feature1, master)" means? – Bernardo Dal Corno Nov 30 '17 at 06:27
  • 4
    @Gauthier If you like to have default colors in your `--pretty` formats, add `%C(auto)` before the element what should be coloured. e.g. `git log --pretty=format:"%cd %h %cn %s %C(auto)%d"` – Radon8472 Feb 05 '18 at 09:11
  • `--decorate` combined with `--pretty=oneline` was exactly what I was looking for, thanks! – thewildandy Sep 24 '22 at 17:07
16

I was looking for something similar to this - but wanted to know what branch a change was made. Hopefully this answer will be of use to others also.

I'm investigating a risk with blackbox encryption, where a repo and it's branches/tags may become unavailable to current admins when enough users leave a project and the keyrings directory has not been religiously based off of master)

I found that the answer below was helpful where the keyrings directory was not updated from master...

Basically adding --source was what I needed to show the branches/tags. Adding --name-only will also show which file was actually changed.

cd /path/to/repo-that-uses-blackbox-encryption
git log --graph --all --decorate --source --name-only keyrings

Another useful answer: How to show git log with branch name

Android Control
  • 496
  • 1
  • 5
  • 14
  • For me, `--source` alone gives `HEAD` for all commits. In combination with `--all`, this seems to distinguish between different branches, but the displayed information is not what I expect: for commits I did in `master`, instead of getting `refs/heads/master`, I get some private branch (`refs/remotes/origin/…`). I suspect that merges and/or new branches make `Git` lose history information. – vinc17 May 10 '20 at 17:36
  • Instead of `--all`, I can filter on the branches with `--branches --remotes=`, but as soon as two branches are merged together, Git no longer has the information on which one had the commit when it was done. – vinc17 May 10 '20 at 17:55
  • `--source` was what I was looking for. – matt Apr 09 '23 at 17:02
7

If you happen to be using oh-my-zsh as your terminal then a bunch of git aliases are available. All of which can be seen at their repo oh-my-zsh/plugins/git. If you don't use this terminal, then you just can grab the aliases and paste the ones you like into your own environment.

The accepted answer presents the git log --graph --all --decorate command, which is available as the glgga alias in oh-my-zsh.

Personally I prefer the glods alias which translates to:

git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ad) %C(bold blue)<%an>%Creset' --date=short
Simon
  • 111
  • 1
  • 7
1

This is an old post but the answers posted don't actually show the branch name of each commit - they only show the branch name for the most recent commit. To list the branch name of every commit use the git show-branch command. For example:

$ git show-branch --all --more
! [Branch_Feature_1] Commit Msg: Feature_1, Commit #1
 ! [Branch_Feature_2] Commit Msg: Feature_2, Commit #2
  ! [Branch_Feature_3] Commit Msg: Feature_3, Commit #1
   * [master] Merge branch 'Branch_Feature_3' Commit Msg: Merge Master Commit #2 + Feature_3
----
   - [master] Merge branch 'Branch_Feature_3' Commit Msg: Merge Master Commit #2 + Feature_3
  +* [Branch_Feature_3] Commit Msg: Feature_3, Commit #1
   * [master^] Commit Msg: Master, Commit #2
 ++* [Branch_Feature_2] Commit Msg: Feature_2, Commit #2
 ++* [Branch_Feature_2^] Commit Msg: Feature_2, Commit #1
+++* [Branch_Feature_1] Commit Msg: Feature_1, Commit #1
+++* [Branch_Feature_1^] Master_Commit_Msg: Original file

To make it clear what each tool shows, I named all branches with a "Branch_" prefix and labeled all commit messages with "Commit Msg".

In this example there are four branches, Master and Branch_Feature_1 through Branch_Feature_3. Branch_Feature_1 has 1 commit and Branch_Feature_2 has two commits. Both were merged via fast-forward merges. Feature_3 has one commit and was merged via a non-FF merge because there was a commit to master after Feature_3 branched.

The lines above the "----" show the most recent commit for each of the branches plus master, ie the tip of every branch. The "*" prefixed before master indicates it's the current active branch. The "!" prefix for all other branches show they're not the active branch.

The lines below the "----" show all the commits for each of the branches. "-" prefix indicates a merge. The "+" and "*" prefix shows which commits are present on which of the branches displayed above the "----". Notice how the columns line up. The "*" vs "+" again is just showing which is the active branch and which aren't.

Compare this to the output of git log for the same repository:

git log --oneline --graph --all --parents
*   2d142e7 fd4f402 9eb9513 (master) Merge branch 'Branch_Feature_3' Commit Msg: Merge Master Commit #2 + Feature_3
|\
| * 9eb9513 c9bb4db (HEAD -> Branch_Feature_3) Commit Msg: Feature_3, Commit #1
* | fd4f402 c9bb4db Commit Msg: Master, Commit #2
|/
* c9bb4db a470d38 (Branch_Feature_2) Commit Msg: Feature_2, Commit #2
* a470d38 f3fde4d Commit Msg: Feature_2, Commit #1
* f3fde4d 81753a3 (Branch_Feature_1) Commit Msg: Feature_1, Commit #1
* 81753a3 Master_Commit_Msg: Original file

Notice how git log only shows the branch names for the commits on the current HEAD for each respective branch. The tree symbols are there to indicate which commits are on which branches but I find the git show-branch output to be much better at showing the relationship.

Note if you prefer to see the SHA-1 values instead of the shorthand references add --sha1-name:

! [Branch_Feature_1] Commit Msg: Feature_1, Commit #1
 ! [Branch_Feature_2] Commit Msg: Feature_2, Commit #2
  * [Branch_Feature_3] Commit Msg: Feature_3, Commit #1
   ! [master] Merge branch 'Branch_Feature_3' Commit Msg: Merge Master Commit #2 + Feature_3
----
   - [2d142e7] Merge branch 'Branch_Feature_3' Commit Msg: Merge Master Commit #2 + Feature_3
  *+ [9eb9513] Commit Msg: Feature_3, Commit #1
   + [fd4f402] Commit Msg: Master, Commit #2
 +*+ [c9bb4db] Commit Msg: Feature_2, Commit #2
 +*+ [a470d38] Commit Msg: Feature_2, Commit #1
++*+ [f3fde4d] Commit Msg: Feature_1, Commit #1
Horshack
  • 111
  • 1
  • 6
0

You can do something like this:

git log --decorate --oneline --pretty='%h refs: %d message:%s'

Notice that not all commits have references to branches or tags. For that you'll need a more complex "plumbing" approach.

Chen Peleg
  • 953
  • 10
  • 16