如何知道“git log”提交属于哪个分支?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7131703/
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-19 05:52:09  来源:igfitidea点击:

How to know which branch a "git log" commit belongs to?

git

提问by Tower

If I do git log, is there any parameter I could specify to be able to tell from the output which branch every commit belongs to?

如果我这样做git log,是否有任何我可以指定的参数能够从输出中判断每个提交属于哪个分支?

Edit: to clarify, I understand that a commit may be part of two branches (for instance). What I want is to get the most recent branch that the commit in logbelongs to. So, if I made a branch called foofrom master. It would belong to both branches, but I want to get foo.

编辑:澄清一下,我知道提交可能是两个分支的一部分(例如)。我想要的是获得提交log所属的最新分支。因此,如果我创建了一个名为foofrom的分支master。它属于两个分支,但我想得到foo.

采纳答案by Luwe

With git logyou already get all the commits from the current branch you are on.

有了git log你已经得到当前分支你在所有提交。

If you want to see commits from merged branches you can use

如果您想查看合并分支的提交,您可以使用

$ git log --pretty=oneline --graph

To create a log tree and see what merged branches a commit stems from.

创建日志树并查看提交源自哪些合并的分支。

--graphwill make the commit tree and --pretty=onelinewill make a one line visualization for every commit

--graph将制作提交树, --pretty=oneline并将为每个提交制作一行可视化

To add branches (as refs) to the log:

向日志添加分支(作为参考):

$ git log --all --source --pretty=oneline --graph

To display branches with commits:

要显示带有提交的分支:

$ git show-branch

回答by Mark Longair

I think that what you're looking for is the very useful command:

我认为您正在寻找的是非常有用的命令:

git branch -a --contains <SHA1sum-of-commit>

... which will tell you every branch (both local and remote-tracking) that contains that commit.

...这将告诉您包含该提交的每个分支(本地和远程跟踪)。

Unfortunately, I don't think there's a git logoption that just outputs this for every commit. Using --all --sourceis close, but will only display oneof the branches for each commit. However, if you click on a commit in gitk --all, you'll see that it lists every branch that that commit is on.

不幸的是,我认为没有一个git log选项可以为每次提交输出这个。使用--all --source很接近,但只会为每个提交显示一个分支。但是,如果您单击 中的提交gitk --all,您会看到它列出了该提交所在的每个分支。

There's one part of your question that isn't very well defined, however - you ask:

但是,您的问题有一部分没有很好地定义 - 您会问:

What I want is to get the most recent branch that the commit in log belongs to

我想要的是获取日志中提交所属的最新分支

It isn't clear to me what you mean by this - the "most recent branch" might be (a) the most recently created ref (b) the most recently modified ref (c) the branch with the most recent commit on it, etc. etc. There's probably a better way of defining what you want in terms of the commit graph.

我不清楚你的意思 - “最近的分支”可能是(a)最近创建的引用(b)最近修改的引用(c)最近提交的分支,等等等等。根据提交图,可能有更好的方法来定义您想要的内容。

回答by holygeek

Have you tried the "--decorate" option to git log?

您是否尝试过 git log 的“--decorate”选项?

I have this alias in my .gitconfig:

我的 .gitconfig 中有这个别名:

[alias]
        k = log --graph --oneline --abbrev-commit  --decorate

It shows a similar graph as the one shown by gitk, with the branch names "decorated" besides the most recent commit in the branch.

它显示了与 gitk 显示的相似的图形,除了分支中最近的提交之外,分支名称“装饰”。