git 命令发出远程跟踪分支的名称

发布于 2024-09-24 16:40:12 字数 255 浏览 4 评论 0原文

我想要一个命令来发出我所在分支的跟踪分支的名称。比如:

$ 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

有这样的命令吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

荆棘i 2024-10-01 16:40:12

根据 Mark Longair 的要求,我之前的评论现在被转载为答案。

使用最新版本的 git,您可以使用 git rev-parse --symbolic-full-name @{u} 发出当前分支的远程跟踪分支的名称。它会发出类似 refs/remotes/origin/master 的内容。

如果您更进一步并使用 --abbrev-ref 标志,如 git rev-parse --symbolic-full-name --abbrev-ref @{u},它会去掉 refs/remotes/ 位,只留下简短的分支名称,例如 origin/master

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.

又怨 2024-10-01 16:40:12

将发出正在跟踪的遥控器:

git config branch.<branchname>.remote

将发出正在该遥控器上跟踪的引用:

git config branch.<branchname>.merge

我不相信有一个组合命令可以同时发出两者(至少在正常的 Git 中;你总是可以自己制作)。


例如,对于本地 master 分支:

$ git config branch.master.remote
origin
$ git config branch.master.merge
refs/heads/master

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
夜光 2024-10-01 16:40:12
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
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
娇纵 2024-10-01 16:40:12

从 git 1.8.3 开始,您现在可以这样做:

git branch -vv 

非常方便,因为它一次显示所有本地分支的跟踪分支,但它不适合编写脚本。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文