是否有工具可以让 git 在图表中显示“分离的头”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16368605/
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
Is there a tool to have git show "detached heads" in a graph?
提问by dmnd
I often refer to git log --graph --decorate --oneline --all --full-history
to see the current state of my branches, but it does not show detached heads/anonymous branches. Is there a way to get detached heads to appear in this graph?
我经常提到git log --graph --decorate --oneline --all --full-history
查看我的分支的当前状态,但它没有显示分离的头/匿名分支。有没有办法让分离的头出现在这张图中?
I know that git reflog
exists, but that is pretty hard to read since there is no structure - all you have to go on is the commit message, which still may be WIP if I haven't finished the commit yet.
我知道它git reflog
存在,但由于没有结构,所以很难阅读——你所要做的就是提交消息,如果我还没有完成提交,它仍然可能是 WIP。
Some background (this is not necessary to answer the question, but will help explain the motivation for it): I'm a Mercurial user and my workflow involves a lot of anonymous branching. I tend to make use of hg heads
a lot to check out these heads, and often hg rebase
to separate or combine series of commits based on what makes sense for the purpose of an easy to understand code review.
一些背景知识(这不是回答问题所必需的,但有助于解释其动机):我是 Mercurial 用户,我的工作流程涉及很多匿名分支。我倾向于使用hg heads
很多方法来检查这些头,并且经常hg rebase
根据对易于理解的代码有意义的目的来分离或组合一系列提交。
While I'm getting used to using git I often find myself with detached heads when, for example, I rebase some commits from a branch to make a new branch. Finding these detached heads is annoying with git reflog
, and to be honest it's a little scary that they just disappear from the usual git log
. I've even forgotten about old commits this way, and have had to dig them out of git reflog
a day or two later. In Mercurial those commits would remain as an anonymous head, and I would be reminded that I need to finish them off.
当我习惯使用 git 时,我经常发现自己的头是分离的,例如,当我从一个分支重新设置一些提交以创建一个新分支时。发现这些分离的头很烦人git reflog
,说实话,它们从平常中消失有点可怕git log
。我什至以这种方式忘记了旧的提交,并且不得不在git reflog
一两天后将它们挖掘出来。在 Mercurial 中,这些提交将保留为匿名负责人,我会被提醒我需要完成它们。
回答by David
It sounds like you're trying to use a workflow that doesn't quite match the way git works.
听起来您正在尝试使用与 git 工作方式不太匹配的工作流程。
First of all, a "detached head" in git isn't the same as Mercurial's concept of a "head". Git has exactly one HEAD, which is just the currently checked-out commit. "Detached" just means that you don't currently have a branch checked out. So when you create a commit, it won't be associated with a branch, and it'll be lost when you checkout a different commit.
首先,git 中的“分离头”与 Mercurial 的“头”概念不同。Git 只有一个 HEAD,它只是当前检出的提交。“分离”仅表示您当前没有签出分支。所以当你创建一个提交时,它不会与一个分支相关联,当你检出一个不同的提交时它会丢失。
In Git, every commit that you care about should be reachable from some branch. There's no such thing as an "anonymous branch", which is why git warns you when committing on a detached head. Committing on a detached head is like allocating an object and then throwing away the pointer; it's possible, but almost never what you want to do. Remember, branches in git are lightweight and can be ephemeral. Just create a branch before committing so that you can find your commits again.
在 Git 中,您关心的每个提交都应该可以从某个分支访问。没有“匿名分支”这样的东西,这就是为什么 git 在提交一个独立的头时会警告你的原因。提交一个分离的 head 就像分配一个对象然后扔掉指针;这是可能的,但几乎从来没有你想做的事。请记住,git 中的分支是轻量级的,并且可以是短暂的。只需在提交之前创建一个分支,以便您可以再次找到您的提交。
All that being said, if you really want to see the structure of your repository, including commits that are only referenced from reflogs, you can use:
话虽如此,如果您真的想查看存储库的结构,包括仅从 reflog 引用的提交,您可以使用:
git log --graph --decorate $(git rev-list -g --all)