14

I'm working on a project which is also worked on by a big team. As time passed by, people have been creating branches and pushing them to a remote repo.

How do I know which branch was the root of a new branch created by another person? New branches can be created based off of current HEAD, but one could also specify <start-point> which can be a commit, a tag, or a branch. How do I find out which branch is used to create a new branch?

A bonus question: commits could be pushed along with a branch creation. I'd like to log those commits pertaining to the new branch by using a post receive hook on the server. The hook is passed with (oldrev, newrev, refname), it would be nice if I can find out answer to my question above.

Eric Wang
  • 301
  • 3
  • 6
  • Branches are pointers, constantly moving to different commits. So by the time you want to check this, the original pointer used to create the branch could have moved on a lot making it impossible to get its name. – poke Jul 15 '13 at 23:49
  • 1
    possible duplicate of [Branch length: where does a branch start in Git?](http://stackoverflow.com/questions/17581026/branch-length-where-does-a-branch-start-in-git) –  Jul 16 '13 at 00:12
  • You can create a branch anywhere. It doesn't have to start from another branch. It can start from an arbitrary commit, including the null commit (which is the parent of the first commit). – Dietrich Epp Jul 16 '13 at 01:47
  • 2
    You might be interested in [this recent thread on the Git list](http://thread.gmane.org/gmane.comp.version-control.git/229422) -- it displays the stance of the Git developers on this issue which basically can be summed up as "if you need branch information in commit objects you did not grasp the mental model of Git yet". (Please note that this does not necessarily represents my personal opinion.) – kostix Jul 16 '13 at 12:05
  • Thanks @kostix. the link you provided solved my mystery – Eric Wang Jul 17 '13 at 00:32

3 Answers3

2

If all branches are created from master you could:

git merge-base master ${branch_in_question}

This only works if no merges have been done since branch creation. For the most part this is not a very easy question to answer in git. Unlike svn log --stop-on-copy. Not that I am saying I like subversion better.

cforbish
  • 8,567
  • 3
  • 28
  • 32
  • yeah, but master is only the default branch. One can create a branch off of any existing branch, since branch is a commit anyway, so one can create a branch from any existing commit. My question was: how do I know which commit was used on the server side? Especially, when user pushed new commits along with the branch creation? – Eric Wang Jul 17 '13 at 00:34
  • Yes I know and is why I stated `If all branches are created from master`. This is question is really not easy to answer for `git`. – cforbish Jul 17 '13 at 13:08
1

There are several ways you can find a common ancestor commit for a group of branches. There are several answers in the question Branch length: where does a branch start in Git?.

One answer (it's one of mine) is to use the show-branch command, passing in as arguments a list of branches that you want to compare and find a common ancestor commit for. Here's an example from the Linux Kernel Git documentation for show-branch

$ git show-branch master fixes mhf
* [master] Add 'git show-branch'.
 ! [fixes] Introduce "reset type" flag to "git reset"
  ! [mhf] Allow "+remote:local" refspec to cause --force when fetching.
---
  + [mhf] Allow "+remote:local" refspec to cause --force when fetching.
  + [mhf~1] Use git-octopus when pulling more than one heads.
 +  [fixes] Introduce "reset type" flag to "git reset"
  + [mhf~2] "git fetch --force".
  + [mhf~3] Use .git/remote/origin, not .git/branches/origin.
  + [mhf~4] Make "git pull" and "git fetch" default to origin
  + [mhf~5] Infamous 'octopus merge'
  + [mhf~6] Retire git-parse-remote.
  + [mhf~7] Multi-head fetch.
  + [mhf~8] Start adding the $GIT_DIR/remotes/ support.
*++ [master] Add 'git show-branch'.

In that example, master is being compared with the fixes and mhf branches. Think of this output as a table, with each branch getting it's own column, and each commit getting its own row. Branches that contain a commit will have a + or - show up in their column in the row for that commit.

At the very bottom of the output, you'll see that all 3 branches share a common ancestor commit, and that it is in fact the head commit of master:

*++ [master] Add 'git show-branch'.

This means that both fixes and mhf were branched off of that commit in master.

Community
  • 1
  • 1
0

How do I know which branch was the root of a new branch created by another person?

While git show-branch can be useful, it might fails for Git 2.12 or less.
The upcoming Git 2.13 (Q2 2017) will increase git show-branch robustness.

See commit d3cc5f4 (15 Feb 2017), and commit d9e557a, commit e6a7c75 (14 Feb 2017) by Jeff King (peff).
Helped-by: Pranit Bauva (pranitbauva1997).
(Merged by Junio C Hamano -- gitster -- in commit 74a7727, 27 Feb 2017)

"git show-branch" expected there were only very short branch names in the repository and used a fixed-length buffer to hold them without checking for overflow.

show-branch: store resolved head in heap buffer

We resolve HEAD and copy the result to a fixed-size buffer with memcpy, never checking that it actually fits.
This bug dates back to 8098a17 (Add git-symbolic-ref, 2005-09-30, Git 0.99.8a).
Before that we used readlink(), which took a maximum buffer size.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250