例如,如果在git上有4个分支,就像这样:
branch1
branch2* (current branch)
branch3 (newest commits here)
master (oldest)
Run Code Online (Sandbox Code Playgroud)
我的问题是:
如何从git命令行检查我当前的分支是否是最新的权限?如果没有,哪个分支是最新的?我的意思是哪个分支包含最新的提交?
如何在没有切换当前分支的情况下列出比我的分支更新的git存储库(在任何分支中)的所有提交?
编辑:您将在我的GitHub仓库中找到相关的shell脚本.
[...]哪个分支是最新的?我的意思是哪个分支包含最新的提交?
我把它解释为
在所有本地分支中,找到其尖端是最新的(在某种意义上,例如就提交者日期而言).
git for-each-ref为此派上用场; 这个瑞士军刀指令值得花时间阅读其手册页.
让我们一步一步构建我们需要的命令......
第一个命令打印所有本地分支的(短)名称列表.
git for-each-ref --format='%(refname:short)' \
refs/heads
Run Code Online (Sandbox Code Playgroud)下一个命令执行相同的操作,但按提交日期按降序对结果进行排序(注意-):
git for-each-ref --sort='-committerdate' \
--format='%(refname:short)' \
refs/heads
Run Code Online (Sandbox Code Playgroud)下一个命令与前一个命令的作用相同,但将输出限制为仅一个条目:
git for-each-ref --count=1 \
--sort='-committerdate' \
--format='%(refname:short)' \
refs/heads
Run Code Online (Sandbox Code Playgroud)总之,我们现在有一个命令可以打印最近(在提交者日期方面)分支引用的(简短)名称:正是我们想要的.大!
为方便起见,我们newest-branch基于它定义一个别名(在下面称为):
git config --global alias.newest-branch "for-each-ref --count=1 --sort='-committerdate' --format='%(refname:short)' refs/heads"
Run Code Online (Sandbox Code Playgroud)
现在,让我们在Git-project repo中运行一些测试:
$ git branch
maint
* master
next
pu
$ git newest-branch
pu
# Now print the list of all branches and the committer date of their tips
$ git for-each-ref --sort='-committerdate' \
--format="%(refname:short)%09%(committerdate)"
refs/heads
pu Tue Mar 17 16:26:47 2015 -0700
next Tue Mar 17 16:14:11 2015 -0700
master Tue Mar 17 16:05:12 2015 -0700
maint Fri Mar 13 22:57:25 2015 -0700
Run Code Online (Sandbox Code Playgroud)
pu确实是最近的分支.好极了!
既然我们拥有newest-branch别名,那就很容易了; 我们所要做的就是将当前分支的名称与最新分支的名称进行比较.(为了更加健壮,您可能希望使用完整的refname而不是short.).
当前分支的(短)名称由.给出
$ git symbolic-ref --short HEAD
master
Run Code Online (Sandbox Code Playgroud)
让我们将它与最新的分支进行比较,然后打印yes当前分支是最新的分支,还是no其他分支.
if [ $(git newest-branch) = $(git symbolic-ref --short HEAD) ]; then
printf "yes\n"
else
printf "no\n"
fi
Run Code Online (Sandbox Code Playgroud)
大!为方便起见,我们也为此定义一个别名(isnewest在下面称为):
git config --global alias.isnewest '! if [ $(git newest-branch) = $(git rev-parse --abbrev-ref HEAD) ]; then printf "yes\n"; else printf "no\n"; fi'
Run Code Online (Sandbox Code Playgroud)
(我们已经知道,从以前的健全性检查来看,这pu是最新的分支.)
$ git branch
maint
* master
next
pu
$ git isnewest
no
$ git checkout pu
Switched to branch 'pu'
Your branch is up-to-date with 'origin/pu'.
$ git isnewest
yes
$ git checkout maint
Switched to branch 'maint'
Your branch is up-to-date with 'origin/maint'.
$ git is-newest-branch
no
Run Code Online (Sandbox Code Playgroud)
似乎工作 (•_•) ( •_•)>??-? (??_?)
如何在没有切换当前分支的情况下列出比我的分支更新的git存储库(在任何分支中)的所有提交?
如果你的意思是某个日期更新,这是一个难题.在最糟糕的情况下,你必须探索整个DAG,因为Git并不禁止父母拥有比他们的孩子更近的日期.如果你的意思是拓扑顺序,那么它就容易多了:
git log --branches HEAD..
Run Code Online (Sandbox Code Playgroud)
(该--branches标志限制输出到可达从所述至少一个提交refs/heads).当然,可以通过添加例如化妆品标志作为调整该命令的输出--oneline,--decorate等等.
| 归档时间: |
|
| 查看次数: |
107 次 |
| 最近记录: |