47

I have a scenario in which there a several remote tracking branches within my local repository that I must sync up to. Our workflow model is:

  • make a branch locally, based off of the desired remote tracking branch
  • make our changes
  • build/test/fix
  • commit
  • push back to the remote server

I've noticed that "git status" doesn't show me what branch my local branch is based on unless something has changed; i.e. uncommitted local changes or a recent fetch puts my local branch behind the times. Is there some way of knowing what branch my local branch is based on without having to change things? Something like, "git status -showparentbranch" or some other command that would show this. Occasionally I run into this need but don't know yet how to satisfy it.

lofidevops
  • 15,528
  • 14
  • 79
  • 119
Andrew Falanga
  • 471
  • 1
  • 4
  • 3

5 Answers5

60

try this:

git log --graph --decorate
Eliran Malka
  • 15,821
  • 6
  • 77
  • 100
elrrrrrrr
  • 894
  • 8
  • 15
  • 14
    You should also add the `--oneline` flag to make the best use of your screen real estate. – cmaster - reinstate monica Oct 15 '15 at 09:16
  • 2
    It's giving the head, author and name of the person who created this branch, but how do we know the branch name which it is created from? – Shailendra Madda Aug 02 '19 at 07:49
  • The oneline is good, except I'm still confused as to how this output helps me? Really? I don't see the parent branch name. What I'm trying to figure out is, did my branch, the one I'm on...get created from develop or master? If it's from develop, and I pull down updates, I should be able to merge them into my branch. But I don't see them. So I think my branch isn't off of develop, but somewhere else, maybe master? I want to verify\confirm this. Git has to be able to help me out with this? – PHenry Mar 16 '23 at 21:39
19

Git does not track what branches a commit was put through. There is no way to tell. If the commits happened on your repo, then you can inspect the reflog, but that's about it. Take a look at the explanation of the DAG in the Pro Git book - also read up on reflog in there.

You can also visualize history better with gitk --all or git log --graph --decorate

Hope this helps.

Elias Zamaria
  • 96,623
  • 33
  • 114
  • 148
Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141
  • 1
    yup. Good catch. You can also add `--oneline` to the log command to fit more commits on the screen. – Adam Dymitruk Jun 23 '11 at 19:05
  • 1
    Many apologies. I forgot to comment about whether or not this answered my question. I'm not sure that it does. From your response, I'm wondering if I asked the question correctly. I wasn't trying to find out what branches were affected by my commit. Rather, I was wondering if there was a way knowing what remote branch, my local branch was based on. – Andrew Falanga Jul 08 '11 at 15:47
  • Beware: `git show-branch` does the job, but spams the screen with pages and pages of changes from the branch. `git log --graph --decorate --oneline` is a great clean option. – thehelix Oct 27 '17 at 15:41
12

git branch -vv will:

  • list all your local branches
  • display the name of the remote branch next to each local branch
  • highlight the active local branch

...from this you will be able to determine the remote branch of the current active branch, and more besides.

If you have a lot of local branches, the list may be very long. Use git branch -vv | grep SOMEWORD to limit the output to only branches containing SOMEWORD. If you can think of a word unique to your branch you'll get the best filter (one result only).

You will also get some additional data in the output, namely the number (SHA1) and message of the last commit. The grep filter will apply to these to. I couldn't find a way to exclude it.

From the Git branch documentation:

-v

-vv

--verbose

When in list mode, show sha1 and commit subject line for each head, along with relationship to upstream branch (if any). If given twice, print the name of the upstream branch, as well (see also git remote show ).

(Based on your comment, yes, it seems that the 'correct' question would ask about the "remote" branch rather than the "parent" branch. But that's what I searched for too! :) )

lofidevops
  • 15,528
  • 14
  • 79
  • 119
  • I'm sorry, I have the same problem as the original poster, and I'm still confused as to how this is helpful? Sure I can see the remote branch names, but the original person is asking what branch the current branch is based off of? In my case, I want to see did I base it off of develop or is it based off of master (cause I screwed up). Please tell me git is able to do this? My concern is, a teammate updated develop, I do a pull and merge and I cannot see that update. How come? I'm assuming cause I'm not based off of develop. Git HAS to be able help me figure this out?! – PHenry Mar 16 '23 at 21:37
2

there are multiple ways to check that. i have endup with Free way to get this result.

  1. use gitkraken(paid for private repository!), it provide you wonderfull way to see Tree of your branchs and commits. by scolling down you will reach root of your parent branch.

  2. if you use bitbucket, then they provide view in web to see all your commits for all branchs(By selecting all branch from dropdown).

  3. Free way, via git commnad.

    git log  --graph --decorate --oneline --all
    

    Commnad Expaination

    git log view log.

    --graph view log as graph.

    --decorate view log in colorfull mode.

    --oneline view log, row only one line not full detail in commits.

    --all view log, all branchs. (important to solve this thread)

chudasamachirag
  • 317
  • 5
  • 14
  • 1
    This command, although looks impressive, doesn't actually show me the parent branch name of the one I'm currently on. That is the original posters question. Or maybe I'm just reading it wrong, but where I see branch names, I don't see develop nor master associated with the one I'm currently interested in. – PHenry Mar 16 '23 at 21:41
0

Here is a command to see the parent branch of your current branch

git show-branch -a | grep '\*' | grep -v `git rev-parse --abbrev-ref HEAD` | head -n1 | sed 's/.*\[\(.*\)\].*/\1/' | sed 's/[\^~].*//'
Arpit
  • 31
  • 6