55

If I do git log, is there any parameter I could specify to be able to tell from the output which branch every commit belongs to?

Edit: to clarify, I understand that a commit may be part of two branches (for instance). What I want is to get the most recent branch that the commit in log belongs to. So, if I made a branch called foo from master. It would belong to both branches, but I want to get foo.

Tower
  • 98,741
  • 129
  • 357
  • 507

3 Answers3

89

I think that what you're looking for is the very useful command:

git branch -a --contains <SHA1sum-of-commit>

... which will tell you every branch (both local and remote-tracking) that contains that commit.

Unfortunately, I don't think there's a git log option that just outputs this for every commit. Using --all --source is close, but will only display one of the branches for each commit. However, if you click on a commit in gitk --all, you'll see that it lists every branch that that commit is on.

There's one part of your question that isn't very well defined, however - you ask:

What I want is to get the most recent branch that the commit in log belongs to

It isn't clear to me what you mean by this - the "most recent branch" might be (a) the most recently created ref (b) the most recently modified ref (c) the branch with the most recent commit on it, etc. etc. There's probably a better way of defining what you want in terms of the commit graph.

Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • 1
    `git branch -a --contains` is a great way to see _all_ the branches containing a specific commit (no matter how many branches there are). If there are more than about 20 branches, then `gitk --all` won't list them, but will just say: `many (22)`. Another solution is `git show-branch --all`, which is awesome, but unfortunately cannot handle more than 29 branches. – TachyonVortex Nov 03 '13 at 18:53
  • 1
    Maybe he means the "closest" or "most direct" branch? That's what I'd like to know. In terms of distance, walk from each named ref to the given commit. List the branches in order from shortest/most direct distance to farthest. – void.pointer Jun 24 '14 at 16:29
  • Just wanted to say "спасибо!" for `--contains`. – Michael Shigorin Jun 08 '20 at 09:59
27

With git log you already get all the commits from the current branch you are on.

If you want to see commits from merged branches you can use

$ git log --pretty=oneline --graph

To create a log tree and see what merged branches a commit stems from.

--graph will make the commit tree and --pretty=oneline will make a one line visualization for every commit

To add branches (as refs) to the log:

$ git log --all --source --pretty=oneline --graph

To display branches with commits:

$ git show-branch
Luwe
  • 3,026
  • 1
  • 20
  • 21
  • Interesting, I updated my question to be more clear this time. – Tower Aug 20 '11 at 12:30
  • 1
    Hmm, in light of your edit, maybe you want to try `git show-branch` instead of `git log`: http://www.kernel.org/pub/software/scm/git/docs/git-show-branch.html – Luwe Aug 20 '11 at 12:55
  • 1
    Nice. Can I get the commit hashes along with `show-branch` so that I could run both commands and combine the results? I need details like username, date, etc that are in `log` but not in `show-branch` unless there's a flag I can specify. – Tower Aug 20 '11 at 14:13
  • Unfortunately that doesn't seem possible with a one line command. I've checked the options for show-branch, but listing extra details is not among them. You would probably need to make an alias (https://git.wiki.kernel.org/index.php/Aliases) or a custom bash script to combine the results. – Luwe Aug 20 '11 at 14:34
  • I am doing this via PHP and I can issue multiple commands, as less as needed for KISS. The `git log --graph` with a custom format is great, except that I still need the "current" branch name. What would be the easiest way to get it? – Tower Aug 20 '11 at 14:55
  • I think this will come closest to what you want: `git log --all --source --pretty=oneline --graph` it will show the refs (the branch) next to the hash. – Luwe Aug 20 '11 at 15:19
  • Do you know if I can get the ref names when using the `format` parameter? I tried `git log --graph --format="%H||%cn||%ce||%cr%d%gD%gs%gd" --all` but it is not quite the same... – Tower Aug 20 '11 at 18:43
  • The format options don't provide extra options for formatting refs: http://www.kernel.org/pub/software/scm/git/docs/git-log.html I've never looked into formatting myself, but it seems it's not possible. – Luwe Aug 20 '11 at 19:07
  • 1
    @Luwe: as I said in my answer (50 minutes before you included the suggestion in an edit), the problem with `--all --source` is that commits can be on multiple branches, and `--source` only shows one of them. `gitk --all` or `git branch -a --contains ` are less likely to mislead. – Mark Longair Aug 21 '11 at 09:46
  • I know, but he only wants one branch. `--all --source` does just that. At least imo it comes closest to what he wants. – Luwe Aug 21 '11 at 09:53
  • `log --all --source` is good, but `show-branch --all` is awesome! @Tower Yes you can get the commit hashes with `show-branch` by using the `--sha1-name` option. – TachyonVortex Nov 03 '13 at 18:41
9

Have you tried the "--decorate" option to git log?

I have this alias in my .gitconfig:

[alias]
        k = log --graph --oneline --abbrev-commit  --decorate

It shows a similar graph as the one shown by gitk, with the branch names "decorated" besides the most recent commit in the branch.

holygeek
  • 15,653
  • 1
  • 40
  • 50