git 如何仅获取一个分支的提交历史?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16974204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 16:21:25  来源:igfitidea点击:

How to get commit history for just one branch?

gitgit-log

提问by Marplesoft

Let's say I created a new branch my_experimentfrom masterand made several commits to my_experiment. If I do a git logwhen on my_experiment, I see the commits made to this branch, but also the commits made to masterbefore the my_experimentsbranch was created.

比方说,我创建了一个新的分支my_experimentmaster并提出了一些对提交my_experiment。如果我执行git logwhen on my_experiment,我会看到对该分支所做的提交,以及master在该my_experiments分支创建之前所做的提交。

I would find it very useful to see the history of all commits to the my_experimentsbranch until it hits the creation of that branch - effectively a true history of just that branch. Otherwise it's not clear to me when looking through the log whether the commits were on the my_experimentsbranch or not.

我会发现查看对my_experiments分支的所有提交的历史记录非常有用,直到它创建该分支为止 - 实际上只是该分支的真实历史。否则在查看日志时我不清楚提交是否在my_experiments分支上。

Is there a way to do this with Git?

有没有办法用 Git 做到这一点?

回答by alex

You can use a rangeto do that.

您可以使用范围来做到这一点。

git log master..

If you've checked out your my_experimentbranch. This will compare where masteris at to HEAD(the tip of my_experiment).

如果你已经检查了你的my_experiment分支。这将比较masterat 到HEAD(的尖端my_experiment)的位置。

回答by cforbish

The git merge-basecommand can be used to find a common ancestor. So if my_experiment has not been merged into master yet and my_experiment was created from master you could:

git merge-base命令可用于查找共同祖先。因此,如果 my_experiment 尚未合并到 master 并且 my_experiment 是从 master 创建的,您可以:

git log --oneline `git merge-base my_experiment master`..my_experiment

回答by VonC

Note: if you limit that log to the last n commit (last 3 commits for instance, git log -3), make sure to put a space between 'n' and your branch:

注意:如果您将该日志限制为最后 n 次提交(例如,最后 3 次提交,git log -3),请确保在“n”和您的分支之间放置一个空格:

git log -3 master..

Before Git 2.1 (August 2014), this mistake: git log -3master..would actually show you the last 3 commits of the current branch (here my_experiment), ignoring the masterlimit (meaning if my_experimentcontains only one commit, 3 would still be listed, 2 of them from master)

在 Git 2.1(2014 年 8 月)之前,这个错误:git log -3master..实际上会向您显示当前分支的最后 3 个提交(此处my_experiment),忽略master限制(意味着如果my_experiment只包含一个提交,则仍会列出 3 个,其中 2 个来自master

See commit e3fa568by Junio C Hamano (gitster):

提交e3fa568通过 JUNIOÇ滨野(gitster

revision: parse "git log -<count>" more carefully

修订:git log -<count>更仔细地解析“ ”

This mistyped command line simply ignores "master" and ends up showing two commits from the current HEAD:

这个输入错误的命令行简单地忽略了 " master" 并最终显示了当前的两次提交HEAD

$ git log -2master

because we feed "2master" to atoi()without making sure that the whole string is parsed as an integer.

Use the strtol_i()helper function instead.

因为我们2masteratoi()没有确保整个字符串被解析为整数的情况下提供“ ” 。

请改用strtol_i()辅助函数。

回答by Daniel Casas

I think an option for your purposes is git log --oneline --decorate. This lets you know the checked commit, and the top commits for each branch that you have in your story line. By doing this, you have a nice view on the structure of your repo and the commits associated to a specific branch. I think reading thismight help.

我认为您的目的的一个选项是git log --oneline --decorate. 这让您知道已检查的提交,以及您故事情节中每个分支的最高提交。通过这样做,您可以很好地了解存储库的结构以及与特定分支关联的提交。我认为阅读本文可能会有所帮助。

回答by Manoj Rana

You can use only git log --oneline

您只能使用 git log --oneline

回答by menssana

I know it's very late for this one... But here is a (not so simple) oneliner to get what you were looking for:

我知道这已经很晚了……但这里有一个(不是那么简单的)oneliner 可以得到你想要的东西:

git show-branch --all 2>/dev/null | grep -E "\[$(git branch | grep -E '^\*' | awk '{ printf  }')" | tail -n+2 | sed -E "s/^[^\[]*?\[/[/"
  • We are listing commits with branch name and relative positions to actual branch states with git show-branch(sending the warnings to /dev/null).
  • Then we only keep those with our branch name inside the bracket with grep -E "\[$BRANCH_NAME".
  • Where actual $BRANCH_NAMEis obtained with git branch | grep -E '^\*' | awk '{ printf $2 }'(the branch with a star, echoed without that star).
  • From our results, we remove the redundant line at the beginning with tail -n+2.
  • And then, we fianlly clean up the output by removing everything preceding [$BRANCH_NAME]with sed -E "s/^[^\[]*?\[/[/".
  • 我们正在列出带有分支名称的提交以及与实际分支状态的相对位置git show-branch(将警告发送到/dev/null)。
  • 然后我们只保留那些带有我们的分支名称的括号内grep -E "\[$BRANCH_NAME"
  • 实际$BRANCH_NAME获得的地方git branch | grep -E '^\*' | awk '{ printf $2 }'(带有星号的分支,没有该星号则回显)。
  • 从我们的结果中,我们删除了开头的冗余行tail -n+2
  • 然后,我们通过fianlly去除在先一切收拾输出[$BRANCH_NAME]sed -E "s/^[^\[]*?\[/[/"