我从哪里分支?

我回到一个旧的项目,我运行了不错的 git status,以弄清楚是怎么回事,我注意到太多的分支!我想做一些家务,然后再开始这项工作,但我不知道哪个分支从哪里来。.

“分支 A”来源于“发展”? “分支 B”来源于“主人”还是“分支 A”?

我如何回答以上的问题?

39983 次浏览

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.

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.

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,

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.

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.

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.

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.

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))'

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.