我使用这个别名
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
以下命令将列出当前fork所指向的远程仓库:
git remote -v
如果要添加远程路径,则可以使用以下命令:
git remote add origin path_name
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)
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
}
但是杰夫·金(Peff)回答道: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.
I think it would print "
gone
" if the upstream branch went missing.
But in this case, the actual local branch is missing. Andfor-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).
我使用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
git remote show origin
。另一个问题的主要答案是一个包装在简单答案周围的Bash脚本,这可能对一些人有用。希望这个问题不会被完全关闭。 - cdunn2001