Where did I branch from?
GitBranchGit BranchGit Problem Overview
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?
Git Solutions
Solution 1 - Git
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 - Git
There's no canonical answer for this, since branches are simply pointers to certain commits in a DAG. For instance, master
and foo
could be pointing at the same commit; if you then create a branch from foo
, it's effectively the same as creating a branch from master
.
That said, if you visualize the commit graph (via gitk
or some other graphical history tool), you can get a general sense of where the branch points are in the commit graph, versus where various branch pointers are pointing.
Solution 3 - Git
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 <super@example.com>
Date: Sun Mar 29 23:14:40 2020 +0000
...
I forked this branch from dev.
Solution 4 - Git
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 5 - Git
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 6 - Git
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 7 - Git
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 8 - Git
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 9 - Git
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.