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
?
First, you can use the "git log --graph --all" option (potentially with "--pretty=format:...") and visually trace back a branch to see what commit it branched from (also referred to as an Ancestor Commit) and then (assuming the history is easy enough to read/understand) trace back up to find out which branch that ...
The branch you added your changes into is called source branch while the branch you request to merge your changes into is called target branch. The target branch can be the default or any other branch, depending on the branching strategies you choose.
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
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With