git command to emit name of remote tracking branch

前端 未结 4 1004
无人共我
无人共我 2020-12-31 01:04

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 t         


        
相关标签:
4条回答
  • 2020-12-31 01:41

    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.

    0 讨论(0)
  • 2020-12-31 01:43
    git config --global alias.show-upstream '!sh -c '\''
    
        test -n "$1" || set -- HEAD
        set -- "$(git rev-parse --symbolic-full-name "$1")"
        git for-each-ref --format="%(upstream:short)" "$1"
    
    
    '\'' -'
    
    git show-upstream
    git show-upstream HEAD
    git show-upstream some/local/branch
    
    0 讨论(0)
  • 2020-12-31 01:45

    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
    
    0 讨论(0)
  • 2020-12-31 01:47

    As of git 1.8.3 you can now do this:

    git branch -vv 
    

    Very convenient as it shows the tracking branch for all local branches at once, but it is not suitable for scripting.

    0 讨论(0)
提交回复
热议问题