git前/后主和分支之间的信息? [英] git ahead/behind info between master and branch?
问题描述
我在本地存储库 (test-branch
) 中创建了一个用于测试的分支,并将其推送到 Github
.
I have created a branch for testing in my local repo (test-branch
) which I pushed to Github
.
如果我去我的 Github
帐户并选择这个 test-branch
它会显示信息:
If I go to my Github
account and select this test-branch
it shows the info:
This branch is 1 commit ahead and 2 commits behind master
我的问题是:
- 如何在本地显示此信息(即:在终端上显示此信息的命令,而不必打开
Github
即可查看)? 我知道我可以使用以下方法查看分支之间的差异:
- How can I display this info locally (ie: a command that shows this on the terminal, rather than having to open
Github
to see it)? I know I can see the diffs between branches using:
git diff master..test-branch
或使用 Meld
(我更喜欢):
or using Meld
(which I prefer):
git difftool master..test-branch
但我想知道是否有办法分别查看 ahead 和 behind 提交.I.E.:有没有办法显示 1 次提交 自己,然后那些 2 次提交 自己?
but I was wondering if there's a way to see the ahead and behind commits separately. I.E.: is there a way to show that 1 commit ahead by itself and then those 2 commits behind by themselves?
推荐答案
Part 1
作为对您的问题 1 的回答,我发现了一个技巧来比较两个分支并显示每个分支领先于另一个分支的提交次数(对您的问题 1 的更笼统的回答):
Part 1
As an answer on your question 1, here's a trick I found to compare two branches and show how many commits each branch is ahead of the other (a more general answer on your question 1):
对于本地分支机构:git rev-list --left-right --count master...test-branch
对于远程分支:git rev-list --left-right --count origin/master...origin/test-branch
输出如下:
2 1
这个输出意味着:与 master
相比,test-branch
提前 1 次提交,落后 2 次提交."
This output means: "Compared to master
, test-branch
is 1 commit ahead and 2 commits behind."
您还可以将本地分支与远程分支进行比较,例如origin/master...master
找出本地分支(此处为 master
)在其远程分支之前/之后的提交次数.
You can also compare local branches with remote branches, e.g. origin/master...master
to find out how many commits a local branch (here master
) is ahead/behind its remote counterpart.
要回答问题的第二部分,解决方案取决于您究竟想要达到什么目的.
To answer the second part of your question, the solution depends on what exactly you want to achieve.
查看提交
为了让 git rev-list
返回任何一侧唯一的提交的确切列表,请将 --count
参数替换为类似 --Pretty=oneline
,使完整的命令执行:
In order to have git rev-list
return the exact list of commits unique on either side, replace the --count
argument with something like --pretty=oneline
, making the complete command to execute:
git rev-list --left-right --pretty=oneline master...test-branch
这将生成如下输出:
<bba27b56ad7072e281d529d4845e4edf877eb7d7 unique commit 2 on master
<dad0b69ec50ea57b076bfecabf2cc7c8a652bb6f unique commit 1 on master
>4bfad52fbcf0e60d78d06661d5c06b59c98ac8fd unique commit 1 on test-branch
这里每个提交 sha 前面都有 <
或 >
以指示它可以在哪个分支上找到(左边或右边,这里是 master
code> 或 test-branch
分别).
Here every commit sha is preceded by <
or >
to indicate which branch it can be found on (left or right, here master
or test-branch
respectively).
查看代码
如果您想查看仅在任一分支上找到的所有新提交的差异,您需要分两步执行此操作:
If you want to view a diff of all new commits only found on either branch, you'll need to do this in two steps:
- 定义最近的共同祖先
$ git merge-base master test-branch
c22faff7468d6d5caef217ac6b82f3ed95e9d902
- 将任一分支与上面获得的提交 sha 进行比较(通常采用短格式)
显示仅在 master 上发现的所有提交的差异
To show the diff of all commits only found on master
git diff c22faff7..master
显示所有提交的差异,只找到 test-branch
To show the diff of all commits only found test-branch
git diff c22faff7..test-branch
这篇关于git前/后主和分支之间的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!