GIT - Where did I branch from?

2019-02-16 04:37发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

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,



回答4:

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.



回答5:

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.



回答6:

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


回答7:

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.