我可以使用 git status
来告诉我是在 Remote 前面还是后面:
git status
On branch master
Your branch is ahead of 'bean/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
哪个适合解析:
needs_push = "Your branch is ahead" in std.out
needs_pull = "Your branch is behind" in std.out
但是,当使用多个 Remote 时,事情就会失败,因为 git status
只显示一个 Remote 的结果(上面示例中的 bean
)并且不接受任何参数选择 Remote 。
有什么方法可以指定要显示详细信息的 Remote 吗? 即给定两个 Remote ,我的本地存储库/分支是否需要推送和/或 pull ,如果需要,哪个?通过“需要 pull ”我的意思是远程包含我没有的提交,通过“需要推送”我的意思是我有远程没有的提交。 (即假设没有任何深奥的线性工作流程。)
如果我可以使用 URL 而不是名称会更好,但据我所知,git 只能在调用 git remote update
后描述 Remote 。
最佳答案
方法A
也许更容易(取决于您想要实现的目标):
-
$ git rev-list
--count <branch-name>..<remote>/<remote-branch-name>
将输出第一个分支在远程分支之后的提交数
-
$ git rev-list
--count <remote>/<remote-branch-name>..<branch-name>
将输出第一个分支在远程分支之前的提交数
使用此命令,您可以列出各种您可能想知道的事情。但是你要明白gitrevisions首先。
$ git rev-list --count <some git revision specification>
方法B
根据您要对这些信息执行的具体操作,以下内容可能会派上用场:
$ git branch -u <upstream>
# short for --set-upsteam-to=<upstream>
设置当前分支的上游$ git status --porcelain=v2 --branch
获得包含有关上游关系的信息的非常容易解析的输出
输出看起来像这样:
# branch.oid <hash>
# branch.head example-branch
# branch.upstream origin/example-branch-developer-1
# branch.ab +0 -1
您最感兴趣的最后一行格式如下:
# branch.ab +<ahead> -<behind>
关于git - 在 Git 状态上选择远程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46828384/