git log 只看当前分支,如何查看一个git分支只返回到分支点?

I noticed when I'm on a branch (we'll call it feat-123) that's only a couple commits in, git log shows the whole history of the branch I branched from (develop). Is there some way to show the log only as far back as the point from which feat-123 was created?

解决方案

There's no shorthand for "find any point where history rejoins", so you have to use something like what Cupcake and D4NI3LS suggested: explicitly stop logging at-or-before such a point.

There is, however, a way to automate this. It can be encapsulated in a "one line" shell script:

git log HEAD --not $(

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

grep -v "^$(git symbolic-ref -q --short HEAD)$"

) "$@"

How this works:

The innermost bit, git symbolic-ref -q --short HEAD, prints the current branch name, or nothing at all if you are not on a branch ("detached HEAD" state).

We turn this into a grep pattern by adding ^ (anchor at start-of-line) and $ (anchor at end).

We use that to remove the current branch (if any) from git for-each-ref output.

The for-each-ref output is the short refname for every branch, i.e., every ref in refs/heads/. Thus, if your complete set of branch names are, for instance, monty, python, flying, and circus and you're on branch flying, this lists monty, python, and circus.

Finally, we give all of this to --not, after asking git log to start at HEAD. The log command uses git rev-list to start from wherever you ask it, and keep going backwards until something (in this case, the --not list) makes it stop. And of course we add "$@" so that any other command-line arguments are included.

Since it's a one liner, it can be embedded in ~/.gitconfig, as an alias:

[alias]

ltf = !git log HEAD --not $(git for-each-ref \

--format='%(refname:short)' refs/heads | \

grep -v "^$(git symbolic-ref -q --short HEAD)$")

and now git ltf, git ltf --oneline, etc., all do the trick. (I don't know if I like the name ltf, it stands for "log to fork" but this is more of a "log to join". Maybe lbr for "log branch"?)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值