How do I determine the source branch of a particular branch?

Viewed 94328

I have a branch in git and want to figure out from what branch it originally was branched and at what commit.

Github seems to know, since when you do a pull request it usually automatically sets up what branch it should go into, but I can't figure out how to do this manually from the command line.

Let me add a concrete example:

master -- ongoing development
2.2    -- stable maintenance

A feature branch feature was created (at commit B below) and worked on (B', C' & E') and merged against the source branch to pick up C and D

 feature branch:    B'-C'-C--D--E'
                   /     /       
 source branch: A--B--C--D--E-- ...

Now I want to merge feature back into its source, but I am not sure whether it was originally a branch off master or 2.2. In order to merge the feature into the correct source, is there a programmatic way to find out if source branch is master or 2.2?

5 Answers

If you've created the branch in your system, you can use git reflog to check the source branch. It will have a line indicating your checkout action. For example:

6f52daa (origin/master, origin/HEAD, master) HEAD@{4}: checkout: moving from master to sample-branch

The github.com user interface can be used to see where a branch was sourced from.

This is one of a number of useful capabilities of the github user interface that is a little bit hidden in that you need to pretend you would like to create a pull request to see the information you are looking for.

To see where a branch came from using the GITHUB.COM user interface:

Navigate to the repository you are working with and select the branches link.
enter image description here

Select "New Pull Request" for the branch you are interested in.
enter image description here

This will take you to a page that shows the source (including the source branch) of your selected branch as well as the changes that have been made from that original source.
enter image description here

Close the window/tab or click away from the page to prevent creating the actual pull request.

Related