Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git command to emit name of remote tracking branch

Tags:

I'd like a command that emits the name of the tracked branch for the branch I'm on. Something like:

$ git checkout --track -b topic origin/master
Branch topic set up to track remote branch master from origin.
Switched to a new branch 'topic'
$ git unknown-command
origin/master

Is there such a command?

like image 743
Russell Silva Avatar asked Sep 21 '10 17:09

Russell Silva


People also ask

What is the name of the remote branch in git?

While “master” is the default name for a starting branch when you run git init which is the only reason it's widely used, “origin” is the default name for a remote when you run git clone . If you run git clone -o booyah instead, then you will have booyah/master as your default remote branch.

How can I tell which branch a branch is from?

Use Status Command Your branch is up to date with 'origin/master'. The first line shows which branch are you on. The second shows your upstream branch if there are any set.


2 Answers

As per Mark Longair's request, my previous comment is now reproduced as an answer.

With recent versions of git, you can emit the name of the remote-tracking branch for your current branch with git rev-parse --symbolic-full-name @{u}. It emits something like refs/remotes/origin/master.

If you go one step further and use the --abbrev-ref flag, as in git rev-parse --symbolic-full-name --abbrev-ref @{u}, it will strip off the refs/remotes/ bit and leave you with just the short branch name, such as origin/master.

like image 54
Lily Ballard Avatar answered Oct 16 '22 04:10

Lily Ballard


Will emit the remote being tracked:

git config branch.<branchname>.remote

Will emit the ref being tracked on that remote:

git config branch.<branchname>.merge

I don't believe that there is a combined command that will emit both together (at least within normal Git; you could always make your own).


For example, for a local master branch:

$ git config branch.master.remote
origin
$ git config branch.master.merge
refs/heads/master
like image 44
Amber Avatar answered Oct 16 '22 03:10

Amber