vdboor
vdboor

Reputation: 22526

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

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.

Upvotes: 255

Views: 110488

Answers (5)

Horshack
Horshack

Reputation: 151

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

Upvotes: 4

Chen Peleg
Chen Peleg

Reputation: 2034

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.

Upvotes: 0

Simon
Simon

Reputation: 121

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

Upvotes: 8

Android Control
Android Control

Reputation: 598

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

Upvotes: 19

CB Bailey
CB Bailey

Reputation: 791691

Try the decorate option.

git log --graph --all --decorate

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

Upvotes: 374

Related Questions