3

I'd like to see what branch my actual branch I'm on is derived from. I already took a look at the log but it's so noisy and I want to get 100% sure to find the correct branch in this case.

xetra11
  • 4,474
  • 6
  • 39
  • 98
  • Martin Nyolt's answer is a workable solution. A good practice to consider in the future is to name your braches with hyphens from the forked branch (i.e. development-newFeature). Or alternatively, you could choose one branch to always branch from (i.e. dev always branches from master, features always branch from dev). But utlimately, this is up to what workflow you decide. – nullverb Sep 30 '16 at 14:38

4 Answers4

5

This problem cannot be answered in every case. Branches are not persistent: you are free to move branches around. The reflog will allow you to see some history of the branch, but the reflog is only local and will be limited to 90 days (by default).

You can use git reflog <branch> to get the first occurrence of this branch in your repo:

$ git reflog branch | tail -n 1
03302d2 branch@{12}: branch: Created from HEAD

In this example, 03302d2 is the first commit of that branch. Then, you can find the branches that the parent of this commit came from. For example, you could do

$ git branch --contains 03302d2^

See the linked question for more sophisticated ways. But note again that this search is not complete and you may miss the "correct" branch.

Community
  • 1
  • 1
Martin Nyolt
  • 3,706
  • 2
  • 22
  • 33
2

The following is getting close, but see Limitations.

git branch --contains $(
   git for-each-ref --format='%(refname)' refs/heads/ |
     xargs git merge-base --fork-point)

Idea

We are using git merge-base --fork-point to obtain a fork point as a SHA-1. merge-base requires a branch name to check against. So you can ask what's the fork-point between master and my current branch fairly easy.

As we want to check against all branches we are using git for-each-ref --format='%(refname)' refs/heads/. We then get a SHA-1 back. In order to get a branch name we are using git branch --contains

Limitations

There are quite a lot of limitations around that. It just gives you a good guess which branches a repository comes from, however for git it is hard to decide which branch something comes from. Take for example

       o -- o
      /     ^ branch-a
o -- X -- o -- o
     \         ^ branch-b
      o -- o -- o
                ^ branch-c

From which branch did branch-c come from? The commit marked as X i on both branches branch-a and branch-b. So it could have been from either of them.

Branches in Git

Branches in Git are simple pointers to commits. Every ancestor of the commit that the branch pointed to is part of the branch: e.g.:

a -- b -- c -- d <master
     \
      d -- e <foo

a,b are part of both master and foo, while b, c, d are part of master. However branches are just names to pointers. We can just set a new name to d.

a -- b -- c -- d <master
      \        ^bar
      d -- e <foo

Now everything on master is also on bar. Branches aren't versioned so we cannot answer the question where branches pointed to at a certain point in time. We therefore cannot clearly decide where foo was branched from. Git offers a reflog that keeps track of branch movements but expires over time.

Conclusion

The only save thing to say in git is the fork-point as a sha-1, due to the nature of branches being mutable. You can however rely on git merge-base --fork-point returning a SHA1 which is the actual fork-point.

somnium
  • 1,082
  • 5
  • 12
  • 1
    Since `--fork-point` *uses* the reflog, this method is also not guaranteed. It's probably simpler to just search the reflog for the "created from" message. If that entry has expired, though, and `--fork-point` finds something, the "something" may be useful. Of course, you can also use the current merge base as a clue (and in fact that's what `--fork-point` falls back on if the reflog entries have expired). – torek Sep 30 '16 at 21:17
2

At the current directory/path of your local repository, Type command:

git log --oneline --decorate --graph

enter image description here (Branch structure of repository https://github.com/spring-projects/spring-boot.git)

You will see the path of your branch.

Do Nhu Vy
  • 33,131
  • 37
  • 143
  • 202
0

To add an alternative way. You can also use git branch with -v

git branch -v

Which will list the originating commit in the second column.

nullverb
  • 82
  • 4