参见: 如何查看哪个Git分支正在跟踪哪个远程/上游分支?
如何知道本地分支正在跟踪哪个远程分支?
我是否需要解析git配置输出,或者是否有一个命令可以为我做这件事?
参见: 如何查看哪个Git分支正在跟踪哪个远程/上游分支?
如何知道本地分支正在跟踪哪个远程分支?
我是否需要解析git配置输出,或者是否有一个命令可以为我做这件事?
当前回答
我使用EasyGit(又名:EasyGit)。"eg")作为Git顶部(或旁边)的超轻量级包装器。EasyGit有一个“info”子命令,它为您提供各种超级有用的信息,包括当前分支的远程跟踪分支。下面是一个例子(当前分支名称是"foo"):
pknotz@s883422: (foo) ~/workspace/bd $ eg info Total commits: 175 Local repository: .git Named remote repositories: (name -> location) origin -> git://sahp7577/home/pknotz/bd.git Current branch: foo Cryptographic checksum (sha1sum): bd248d1de7d759eb48e8b5ff3bfb3bb0eca4c5bf Default pull/push repository: origin Default pull/push options: branch.foo.remote = origin branch.foo.merge = refs/heads/aal_devel_1 Number of contributors: 3 Number of files: 28 Number of directories: 20 Biggest file size, in bytes: 32473 (pygooglechart-0.2.0/COPYING) Commits: 62
其他回答
如果你正在使用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
}
您可以使用git签出,即。“签出当前分支”。如果存在当前分支的跟踪信息,这是一个没有副作用的操作。
$ git checkout
Your branch is up-to-date with 'origin/master'.
如果你想找到任何分支的上游(而不仅仅是你所在的分支),这里是对@cdunn2001的答案的轻微修改:
git revv -parse——abbrev-ref——symbol -full-name
这将为名为YOUR_LOCAL_BRANCH_NAME的本地分支提供远程分支名称。
你可以试试这个:
git remote show origin | grep "branch_name"
Branch_name需要替换为您的分支
我认为git branch -av只告诉你你有什么分支,它们在哪个提交,让你推断本地分支正在跟踪哪些远程分支。
Git remote show origin显式地告诉你哪个分支正在跟踪哪个远程分支。下面是一个存储库的输出示例,包含一个提交和一个名为branch的远程分支:
$ git branch -av
* abranch d875bf4 initial commit
master d875bf4 initial commit
remotes/origin/HEAD -> origin/master
remotes/origin/abranch d875bf4 initial commit
remotes/origin/master d875bf4 initial commit
与
$ git remote show origin
* remote origin
Fetch URL: /home/ageorge/tmp/d/../exrepo/
Push URL: /home/ageorge/tmp/d/../exrepo/
HEAD branch (remote HEAD is ambiguous, may be one of the following):
abranch
master
Remote branches:
abranch tracked
master tracked
Local branches configured for 'git pull':
abranch merges with remote abranch
master merges with remote master
Local refs configured for 'git push':
abranch pushes to abranch (up to date)
master pushes to master (up to date)