74

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?

Mel
  • 5,837
  • 10
  • 37
  • 42
Arne Claassen
  • 14,088
  • 5
  • 67
  • 106
  • 2
    See [how can I find the revision at which a branch was created?](http://stackoverflow.com/q/6058308/11343) – CharlesB Jun 16 '11 at 16:11
  • Try rephrasing your question to not use the ambiguous term "refreshed". It has not accepted meaning as far as I know. Perhaps you mean "rebase"? or "merge"? – Frosty Jun 21 '11 at 14:35
  • @Frosty -- i've clarified and from your answer it sounds like the information I'm looking for does not exist. I can only "divine" the origin by looking at the commits or graph and make an educated guess. – Arne Claassen Jun 22 '11 at 19:27

5 Answers5

38

Git only tracks "upstream" information in individual repositories and this information is not static nor shared between separate clones of the same repository.

The command to set this relationship from the command line is:

git branch --set-upstream <branch> [<start-point>]

Looking at the output of git-diff might give you a clue:

git diff <mybranch>..master # commits in master that are not in mybranch
git diff <mybranch>..2.2 # commits in 2.2 that are not in mybranch

It is likely that the one with fewer commits listed is the branch point (but that is not guaranteed, obviously.

You can also use gitk, or git log to take a look around:

gitk --all
git log --graph --color --decorate --oneline --all
Frosty
  • 6,213
  • 3
  • 24
  • 20
  • 1
    I take your answer to mean that creating a branch merely is a pointer to start new commits from, so i can track down common commits between branches, but commits don't "belong" to branches so it's not possible to find the commit that was started the current branch and to what branch it belonged at the time. – Arne Claassen Jun 22 '11 at 19:30
  • 3
    @Arne Claassen: Yes. Branches are just a pointer to a commit. And that commit points to its own parent, and so on. – Daenyth Jun 22 '11 at 20:00
  • 1
    `git log --graph --color --decorate --oneline --all` seems to be the one that let's me figure it out the easiest, but it's certainly no simple answer. I wonder how github knows what branch a pull request should go against. Do they just have an algorithm that makes an educated guess? – Arne Claassen Jun 22 '11 at 20:48
8
git branch -r

With --contains, shows only the branches that contain the named commit (in other words, the branches whose tip commits are descendants of the named commit). With --merged, only branches merged into the named commit (i.e. the branches whose tip commits are reachable from the named commit) will be listed. With --no-merged only branches not merged into the named commit will be listed. If the argument is missing it defaults to HEAD (i.e. the tip of the current branch).

Considering that the previous commit has been already pushed to the remote branch, you can filter available remote branches using --contains key.

# git branch -r --contains HEAD~1
  origin/HEAD -> origin/master
  origin/master

or

git branch -r --contains refs/heads/<local_branch_name>~1

If you have 2 non-pushed commits yet than change the number accordingly in order to reach already pushed commit:

# git branch -r --contains HEAD~2
  origin/<parent_remote_branch>

If the amount of local non-pushed commits is unknown you may cycle through until you get the result, for example:

#!bin/bash

i=0;
res=; 
while [ -z "$res" ]; do 
    res=$(git branch -r --contains HEAD~$i); 
    echo "$i"; 
    i=$(($i+1)); 
done

echo "$res"

Of course you can limit iterations amount to be on safe side.

Igor Tverdovskiy
  • 847
  • 1
  • 7
  • 5
  • This helps me more than the others but for me was like, after using, `git branch --contains ` and if it was correct it appear in green, with the name of the current branch prepended with an asterix – AssertionError Sep 22 '22 at 12:52
8

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
therealak12
  • 1,178
  • 2
  • 12
  • 26
  • 1
    This is helpful, something like: `git reflog | grep -E "moving from .+? to $(git rev-parse --abbrev-ref HEAD)" | sed -re 's|^.+moving from ([^ ]+).+$|\1|'` to output the "from" branch. Of course, only useful if _you_ created the branch. If you didn't you can try to get the upstream branch from the config: `git config branch.${CURRENT_BRANCH}.merge` With those two, you can check which branch is already on the remote and that "might be" the one you want to compare against. – Josh M. Feb 16 '23 at 07:19
4
git show-branch [--all]
git merge-base

The first will show you branches and give you merge information. The second will help you understand the relationship between two specific branches (where they last diverged).

Seth Robertson
  • 30,608
  • 7
  • 64
  • 57
0

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.

John
  • 3,458
  • 4
  • 33
  • 54