Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know which branch a "git log" commit belongs to?

Tags:

git

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.

like image 725
Tower Avatar asked Aug 20 '11 12:08

Tower


People also ask

How do you find which branch a commit belongs to?

It is based on "Find merge commit which include a specific commit". Find when a commit was merged into one or more branches. Find the merge commit that brought COMMIT into the specified BRANCH(es). Specifically, look for the oldest commit on the first-parent history of BRANCH that contains the COMMIT as an ancestor.

Do commits belong to a branch?

As you start making commits, you're given a master branch that points to the last commit you made. Every time you commit, the master branch pointer moves forward automatically. The “master” branch in Git is not a special branch. It is exactly like any other branch.

Does git log show all branches?

Graph all git branchesDevelopers can see all branches in the graph with the –all switch. Also, in most situations, the –decorate switch will provide all the supplemental information in a formatted and nicely color-coded way.


2 Answers

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.

like image 115
Mark Longair Avatar answered Oct 22 '22 06:10

Mark Longair


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
like image 27
Luwe Avatar answered Oct 22 '22 06:10

Luwe