Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find out the git branch my current branch is based on?

Tags:

git

branch

This might be a simple question (maybe asked before?), but I haven't been able to find an answer (I really tried, I'm not just lazy :) ).

I am working on a git branch that is based on another branch. However, I don't know which branch it is based on.

Is there a command or a way to find out which branch my current branch originates from?

I tried looking in SourceTree and also with git log --graph --all but somehow I can't really figure out the originating point when the branch was created.

Thanks!

like image 463
Sebastian Avatar asked Oct 31 '14 17:10

Sebastian


People also ask

How can I tell which remote branch is being tracked?

There is a command that gives you about all tracking branches. And to know about the pull and push configuration per branch you can use the command git remote show origin. and you can use -sb option for seeing the upstream. Hope this information will help you to find which branch is tracking.

How do I find my parent branch?

to find the parent of the branch, if you add the Joe Chrysler's answer as a Git alias. It will simplify the usage. Open the gitconfig file located at "~/. gitconfig" by using any text editor (for Linux).


1 Answers

There really is no such notion in git in the first place.

This is perhaps best seen by example. Consider the following commit graph fragment:

        o    <-- branchA
       /
o--o--o
    \
     o--o    <-- branchB

Here, "obviously" branchB comes from branchA. But wait, there's more, there's a bit I left out:

        o    <-- branchA
       /
o--o--o--o   <-- branchC
    \
     o--o    <-- branchB

Now, does branchB comes off branchA or does it come off branchC?

The trickiest part is that any of these branches can be created in any order;1 and furthermore, the labels—the branch names—can also be moved or removed at any time.2 So if you decide that branchB is "based off" branchA, but then someone deletes branchA entirely, you're left with this:

o--o--o--o   <-- branchC
    \
     o--o    <-- branchB

and now branchB is clearly based off branchC, not branchA.

[Edit: if you want to identify the specific commit where two branches first "split apart", use git merge-base. Having found that commit, you can see what other branch names might also be interesting with git branch --contains, and so on. The general rule here is that the commit graph is all you really have: labels like branch names are only good until you or someone else changes them later. Tag labels should generally stay where they are but even those can be moved, it's just generally more of a pain.]


1Well, almost any: the parent commit(s) of a new commit must exist before you can create the child commits, unless you have a way of breaking the SHA-1 crypographic hash.

2Removing a label means that the commits found by starting at that label and working "backwards" (left-ward, and maybe up or down as well as long as you keep moving leftward) in these drawings) become "unreachable", unless they're found by starting from some other label. Unreachable commits in the commit-graph are eventually3 removed entirely, but typically you have about 30 days to get them back.

3This is achieved with git's "reflogs". Each reflog entry works like a regular label, in terms of making a commit "reachable" and therefore retaining it. It's actually the reflog entries that are expired.

like image 178
torek Avatar answered Sep 19 '22 17:09

torek