23

I try git log w/ --decorate and --source options. But still can not get the branch name of commit 2f3cb60 and d7e7776, Why?

#git log 2f3cb60 --graph  --decorate --source --all --oneline
...
* | | | 1920ad5 refs/heads/gpio support gpio lib
| |/ /
|/| |
* | | 2f3cb60   2f3cb60 fix
* | | d7e7776   2f3cb60 fix
| |/
|/|
* | aa4dcfb     refs/remotes/origin/httpd support
* | cfc839d     refs/remotes/origin/httpd add folder

How do I show git log with branch name?

Eric Leschinski
  • 123,728
  • 82
  • 382
  • 321
Robber Pen
  • 837
  • 2
  • 6
  • 19

1 Answers1

31
$ git log --graph --decorate --oneline
* 1f3e836 (HEAD, origin/v2, v2) Change scripts to new format.
*   34d458f (origin/master, master) Merge branch 'new-shell'
|\  
| * 995ece7 (origin/new-shell) Fix index.html and add script pushing.
| * fe0615f New shell hello-world.
|/  
* fe1b1c0 Progress.
...

git log --graph --decorate --oneline should show you names of the commits that have names. Not every commit is associated with a branch name.

Remember, a branch name is just a pointer to a particular commit. Each commit has a parent, so one commit may be a part of the history of a dozen separate branches.

  • You can see which branches contain a commit via git branch --contains <ref>.
  • If you just need some kind of symbolic name to track down a commit, use git name-rev <ref>.
  • If you need a shell-scriptable ("plumbing") list of all branches containing a commit, try this:

    commit=$(git rev-parse <ref>) # expands hash if needed
    for branch in $(git for-each-ref --format "%(refname)" refs/heads); do
      if git rev-list "$branch" | fgrep -q "$commit"; then
        echo "$branch"
      fi
    done
    

See also: SO: Finding what branch a commit came from

Community
  • 1
  • 1
Jeff Bowman
  • 74,544
  • 12
  • 183
  • 213
  • What about simply `git branch --contains `? What does the `COMMIT= ... for ... done` shell scripting do that `git branch --contains` does not? – Colin D Bennett Oct 23 '13 at 17:48
  • 1
    @Colin `git branch --contains` is great! That's why it's the first bullet of my three listed options. :) The thing is that `git branch` doesn't have a guaranteed output—it could change format in future releases—so you might not want to use it in long-lived scripts. This is part of the [plumbing vs porcelain](http://git-scm.com/book/ch9-1.html) separation: If you're a human reading the output, you can use porcelain commands like `git branch`, but if you're writing a tool you'll want to use plumbing commands like `for-each-ref` and `rev-list` that guarantee their output format. – Jeff Bowman Oct 23 '13 at 17:59
  • @JeffBowman Thanks for pointing that out, I glossed over your comment "If you need a shell-scriptable...". It would be great to have a `--porcelain` option for `git branch` to simplify this, but thanks for the script snippet! – Colin D Bennett Oct 23 '13 at 20:20
  • 1
    @JeffBowman why `Not every commit is associated with a branch name`? My understanding is that when we commit, we commit to a specific branch and that branch should be the one associated to the commit, correct? – Qi Zhang Mar 05 '19 at 21:42
  • 1
    @QiZhang A branch is an auto-advancing pointer to a commit: when you commit, it might go on a branch, yes, but that is temporary. You can reset the branch to point to any commit you'd like, and the branch you were on when you committed isn't a permanent part of the commit unless you write it down in the commit log yourself. A commit might be associated with several branches at once, or none at all, and for most commits you care about it is probably a parent or ancestor of one or more branches, so you have [several names you can use](https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection). – Jeff Bowman Mar 06 '19 at 07:22