Andrew Falanga
Andrew Falanga

Reputation: 501

How can I tell which remote "parent" branch my branch is based on?

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:

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.

Upvotes: 50

Views: 112458

Answers (7)

user2039709
user2039709

Reputation: 970

A perhaps simpler (recent) alternative (mine): https://stackoverflow.com/a/79132926/2039709

git merge-base main $1 \
| xargs -I{} git branch --sort=creatordate --contains {} \
| grep -Ev "main|$1" \
| head -n 1

Upvotes: 1

S G
S G

Reputation: 51

This is a copy of Arpit's answer but with an explanation.

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

This command chain is used to find the most recent branch that is not the current branch. Let's break it down step-by-step:

  1. git show-branch -a:

    • Displays the commit ancestry graph of all branches and references.
  2. grep '\*':

    • Filters the output to show only the lines that contain an asterisk (*). In git show-branch, the asterisk indicates the current branch or a branch that contains the commit.
  3. grep -v \git rev-parse --abbrev-ref HEAD``:

    • Excludes the lines that correspond to the current branch. git rev-parse --abbrev-ref HEAD returns the name of the current branch, and grep -v excludes lines that match this name.
  4. head -n1:

    • Takes the first line from the remaining lines. This should be the most recent branch that is not the current branch.
  5. sed 's/.*\[\(.*\)\].*/\1/':

    • Uses sed (stream editor) to extract the branch name from the line. The pattern matches everything between square brackets ([...]) and captures it.
  6. sed 's/[\^~].*//':

    • Further processes the branch name to remove any characters following ^ or ~. These characters are used in Git to refer to ancestors of commits (e.g., branch^ is the parent of the tip of the branch, branch~1 is the first ancestor, etc.).

Upvotes: 1

Arpit Chokniwal
Arpit Chokniwal

Reputation: 111

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/[\^~].*//'

Upvotes: 3

chudasamachirag
chudasamachirag

Reputation: 327

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)

Upvotes: 3

Adam Dymitruk
Adam Dymitruk

Reputation: 129526

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.

Upvotes: 19

elrrrrrrr
elrrrrrrr

Reputation: 944

try this:

git log --graph --decorate

Upvotes: 65

lofidevops
lofidevops

Reputation: 16942

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! :) )

Upvotes: 13

Related Questions