'Where did I branch from?

I got back on an old project and I ran the nice git status to figure out what was going on and I noticed way too many branches! I want to do some housekeeping before starting to work on this again but I'm not sure which branch comes from which..

E.G. Does "branchA" derive from "develop"? Does "branchB" derive from "master" or "branchA"??

How can I answer the sample questions above?



Solution 1:[1]

git merge-base shows the commit that is the common ancestor of two branches.

Simple usage: git merge-base <branch> <branch> shows the common commit of the two branches.

Solution 2:[2]

If you are already on a branch then you can get the commit that is the point where it forked from another branch, say master, like this:

git merge-base --fork-point master

Then fetch the commit message with git show <commit-id>. If you got no commit ids then this branch did not come from that.

For example I'm not sure if I forked from dev or master:

$ git checkout my-branch
$ git merge-base --fork-point master
$ git merge-base --fork-point dev
1770f75fa178df89a40ddc5f13ecd6bc597c17df
$ git show 1770f75fa178df89a40ddc5f13ecd6bc597c17df
commit 1770f75fa178df89a40ddc5f13ecd6bc597c17df (origin/popup-stack-12, dev)
Merge: 37f79cf f8a9795
Author: Superman <[email protected]>
Date:   Sun Mar 29 23:14:40 2020 +0000
...

I forked this branch from dev.

Solution 3:[3]

As others mentioned, there's no canonical answer, but git reflog can help in some cases by showing your local git history:

a1b2c3d4 (HEAD -> my_current_working_branch) HEAD@{0}: pull origin master: Merge made by the 'recursive' strategy.
aaaa1111 (origin/my_current_working_branch) HEAD@{1}: commit: Added bar
def123aa (forgotten_branch) HEAD@{2}: checkout: moving from forgotten_branch to my_current_working_branch

In this case I can see I branched from forgotten_branch to my_current_working_branch, and merged master in.

It won't always give you the information you're looking for, but can help in certain situations.

Solution 4:[4]

The command line git executable also has the ability to display a rudimentary tree of branching and merging, use the --graph option to git log. You can do all sorts of nice enhancements to it with the other options. Try these on for size:

simplest:

git log --graph

nice:

git log --graph --decorate --simplify-by-decoration --color --oneline --date=local

the full monty:

git log --graph --decorate --simplify-by-decoration --color --oneline --date=local --pretty=format:'%C(auto) %h %d %C(reset)%s (%C(cyan)%ad %ae%C(reset))'

Solution 5:[5]

You can use a graphical tree viewer, I'm using gitg to view branches and diffs, although I'm using the command line for the real work most of the time.

Solution 6:[6]

If you are working on Windows or Linux (with GUI), just install the beautiful git-extensions. They can visualize you the branch / merges tree perfectly fine.

http://code.google.com/p/gitextensions/downloads/detail?name=GitExtensions207.zip&can=4&q=

Greetings,

Solution 7:[7]

Stick to a naming convention and spare yourself all the confusion.

When creating a branch from master - lets say, to implement an email feature - you can name it master_emailfeature. Then if you need to create a sub-branch from this branch to implement ssl for emails then you can name it master_emailfeature_sslandtls.

This makes it clear which branch was created from which just by looking at the branch's name.

Solution 8:[8]

If you want to figure out which remote branch your local branch derived from, this command can shed some light.

git remote show origin

Scroll down until you see "Local branches configured for 'git pull':" and underneath that, a list of all your local branches are displayed and the remote branch each will merge with.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 technocrat
Solution 2 John Mee
Solution 3 JerkyTreats
Solution 4 smonff
Solution 5 Not_a_Golfer
Solution 6 Mario Fraiß
Solution 7 Basil Musa
Solution 8 Jake Levsen