找出本地分支正在追踪哪个远程分支

1234

40
天啊,这并不是完全重复的。这是另一个问题的子集,但是还有其他方法可以提出这个问题,比如git remote show origin。另一个问题的主要答案是一个包装在简单答案周围的Bash脚本,这可能对一些人有用。希望这个问题不会被完全关闭。 - cdunn2001
8
同意,这绝对不应该是重复的。它询问的内容与链接问题完全不同。 - Adam Batkin
26个回答

1

我使用这个别名

git config --global alias.track '!sh -c "
if [ \$# -eq 2 ]
 then
   echo \"Setting tracking for branch \" \$1 \" -> \" \$2;
   git branch --set-upstream \$1 \$2;
 else
   git for-each-ref --format=\"local: %(refname:short) <--sync--> remote: %(upstream:short)\" refs/heads && echo --URLs && git remote -v;
fi  
" -'

那么

git track

请注意,该脚本也可用于设置跟踪。 https://github.com/orefalo/bash-profiles 上有更多优秀的别名。

1

以下命令将列出当前fork所指向的远程仓库:

git remote -v

如果要添加远程路径,则可以使用以下命令:

git remote add origin path_name


这里你不是在查找远程路径 - 你正在添加。 - serup

0
git branch -vv | grep 'hardcode-branch-name'
# "git rev-parse --abbrev-ref head" will get your current branch name
# $(git rev-parse --abbrev-ref head) save it as string
#  find the tracking branch by grep filtering the current branch 
git branch -vv | grep $(git rev-parse --abbrev-ref head)

1
一个好的答案总是会包括为什么这样会解决问题的解释,这样原帖作者和以后的读者都可以从中学习到知识。 - Tyler2P

0
如果你正在使用Gradle,
def gitHash = new ByteArrayOutputStream()
    project.exec {
        commandLine 'git', 'rev-parse', '--short', 'HEAD'
        standardOutput = gitHash
    }

def gitBranch = new ByteArrayOutputStream()
    project.exec {
        def gitCmd = "git symbolic-ref --short -q HEAD || git branch -rq --contains "+getGitHash()+" | sed -e '2,\$d'  -e 's/\\(.*\\)\\/\\(.*\\)\$/\\2/' || echo 'master'"
        commandLine "bash", "-c", "${gitCmd}"
        standardOutput = gitBranch
    }

0
这个2023年第四季度的帖子讨论了一些特殊情况:
  • 新的空克隆仓库
  • 或者本地分支不存在

it does not work for a fresh clone of an empty repository

git for-each-ref --format="%(upstream:short)" refs/heads/master

outputs nothing, while

git status -b --no-ahead-behind --porcelain=v2

outputs

# branch.oid (initial)
# branch.head master
# branch.upstream origin/master

I.e. it outputs a proper upstream branch.

但是杰夫·金(Peff)回答道:

I think it would print "gone" if the upstream branch went missing.
But in this case, the actual local branch is missing. And for-each-ref will not show an entry at all for a ref that does not exist.
The "refs/heads/master" on your command line is not a ref, but a pattern, and that pattern does not match anything. So it's working as intended.

I think a more direct tool would be:

git rev-parse --symbolic-full-name master@{upstream}

That convinces branch_get_upstream() to return the value we want, but, sadly, it seems to get lost somewhere in the resolution process, and we spit out an error. Arguably, that is a bug (with --symbolic or --symbolic-full-name, I think it would be OK to resolve names even if they don't point to something, but it's possible that would have other unexpected side effects).


-5

我使用EasyGit(又名“eg”)作为Git的超轻量级包装器。EasyGit有一个“info”子命令,可以提供各种超级有用的信息,包括当前分支的远程跟踪分支。以下是一个示例(其中当前分支名称为“foo”):

pknotz@s883422: (foo) ~/workspace/bd $ eg info 总提交数:175 本地仓库:.git 命名的远程仓库:(名称 -> 位置) origin -> git://sahp7577/home/pknotz/bd.git 当前分支:foo 密码校验和(sha1sum):bd248d1de7d759eb48e8b5ff3bfb3bb0eca4c5bf 默认拉取/推送仓库:origin 默认拉取/推送选项: branch.foo.remote = origin branch.foo.merge = refs/heads/aal_devel_1 贡献者数量:3 文件数量:28 目录数量:20 最大文件大小(以字节为单位):32473(pygooglechart-0.2.0/COPYING) 提交数:62

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接