使用git for-each-ref和git log在源头上显示每个远程分支上的最新提交 [英] Show most recent commit on each remote branch on origin using `git for-each-ref` and `git log`

查看:85
本文介绍了使用git for-each-ref和git log在源头上显示每个远程分支上的最新提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经完成了git fetch --all --prune从远程存储库中获取引用的操作,现在我想查看origin

Having done git fetch --all --prune to fetch refs from the remote repositories I now want to see the most recent commit on each of the branches on origin

命令

git for-each-ref --format='%(refname:short)' | grep 'origin/'

列出了origin上的所有十六个分支,因此我希望以下命令显示每个分支中的最新提交

lists all sixteen branches on origin, and so I would expect the following command to show the most recent commit from each one

git for-each-ref --format='%(refname:short)' | grep 'origin/' | 
xargs git log --source -n 1 

(注:这是我运行时的一行;出于可读性考虑,我将其分成两行.)

(N.B. This is one line when I run it; I have split it over two for readability.)

但是该命令不起作用,它仅列出一个提交:似乎一个提交限制适用于整个列表,而不适用于每个分支.

However the command does not work, it only lists one commit: it seems the one commit limit applies to the entire list and not to each branch in turn.

我在做什么错了?

(请注意,我也在此处

git for-each-ref --format='%(refname:short)' | grep 'origin/' | 
xargs git show --oneline 

,但--oneline标志将被忽略,结果太冗长.)

but the --oneline flag is ignored and the result is too verbose.)

推荐答案

问题是git log只打印一个引用的日志,这就是为什么您的xargs调用不起作用的原因.如果您例如调用git log -n 1 origin/master origin/not_master(使用xargs将导致这样的调用),您将获得最新提交的日志,然后该列表仅限于第一个条目.我不知道如果两个分支分开会发生什么,但是您仍然会将完整的日志输出限制为一个条目.

The problem is that git log only prints the log of one ref, that is why your xargs call is not working. If you e.g. call git log -n 1 origin/master origin/not_master (using xargs will result in a call like this) you'll get the log up to the most recent commit and that list is then limited to the first entry. I don't know what exactly will happen if the two branches have diverged, but still, you'll limit the complete log output to only one entry.

除了VonC的详细答案外,如果将git log命令包装在循环中而不是使用xargs,则可以修改命令以获得所需的结果:

Additionally to VonC's detailed answer, you can modify your command to achieve the desired result if you wrap the git log command in a loop instead of using xargs:

for ref in $(git for-each-ref --format='%(refname:short)' | grep 'origin/')
do
    git --no-pager log $ref -n 1 --oneline
done

使用--no-pager选项将输出直接发送到控制台.

Use the --no-pager option to send the output directly to the console.

这篇关于使用git for-each-ref和git log在源头上显示每个远程分支上的最新提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆