如何获取(已删除)分支的 git log 显示名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12114519/
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
how to get git log display name of (deleted) branches
提问by Davide
The way I like to see my git logs are with
我喜欢看我的 git 日志的方式是
git log --graph --oneline --all --decorate
Among other things that I found useful its output, there are the branch names. However, if I delete a branch, then the above does not display them anymore. I mean seeing a bunch of stuff like:
在我发现其输出有用的其他内容中,还有分支名称。但是,如果我删除一个分支,则上面不再显示它们。我的意思是看到一堆东西,比如:
* 87c3294 (QueueExample) blah blah
is much more expressive (especially when the list becomes long) than a bunch of
比一堆更具表现力(尤其是当列表变长时)
* 87c3294 blah blah
The answers to this questionand in particular this commentseems to imply that the branch names are still "somewhere".
这个问题的答案,特别是这个评论似乎暗示分支名称仍然是“某处”。
How do I get them printed in the output of git log
or at least in some other way?
我如何将它们打印在输出中git log
或至少以其他方式打印?
Alternatively, how can I remove branches from the output of git branch, while still keeping them around for purpose of git log
?
或者,如何从 git branch 的输出中删除分支,同时仍然保留它们以用于git log
?
回答by Ethan Brown
In Git, branches are simply pointers to a commit that move as new commits are added on that branch. In other words, once the pointer has moved, there is no memory that previous commits were on that branch. This was a hard concept for me to wrap my head around at first. Perhaps it's the name: "branch" makes me think of multiple nodes connected by edges, but in Git, a branch is really only a moving pointer to a node.
在 Git 中,分支只是指向一个提交的指针,随着新提交添加到该分支上而移动。换句话说,一旦指针移动,之前的提交在那个分支上就没有内存了。起初,这对我来说是一个很难理解的概念。也许是这个名字:“分支”让我想到了由边连接的多个节点,但在 Git 中,分支实际上只是一个指向节点的移动指针。
git log
dutifully annotates commits with any branch that is pointing to them. For example, I created a repo with commits "one", "two", and "three" on branch master and "uno", "dos", and "tres" on branch feature, then merged feature back into master. Here's what git log
tells me before I delete the branch:
git log
尽职尽责地用指向它们的任何分支注释提交。例如,我创建了一个 repo,在 master 分支上提交了“one”、“two”和“three”,在分支 feature 上提交了“uno”、“dos”和“tres”,然后将功能合并回 master。这是git log
我删除分支之前告诉我的信息:
* 9eb6e93 (HEAD, master) Merge branch 'feature'
|\
| * 523e2ac (feature) tres
| * 6d3cc0f dos
| * 1bc0b2e uno
* | d39734b three
* | 779d37b two
* | facbcbf one
|/
* 58848f4 Initial commit.
It's easy to get fooled into thinking that the "(feature)" annotation is somehow referring to that branch on the right, but it's not: it's just referring to the commit 523e2ac
.
很容易被愚弄,认为“(功能)”注释以某种方式指的是右侧的那个分支,但事实并非如此:它只是指的是 commit 523e2ac
。
Note that, by default, when Git creates a merge commit (9eb6e93
in our case), it automatically adds a comment stating that it's merging branch 'feature', so there is some record of there having been a branch there, but it's just a comment, nothing more.
请注意,默认情况下,当 Git 创建一个合并提交(9eb6e93
在我们的例子中)时,它会自动添加一个注释,说明它正在合并分支“功能”,所以有一些记录表明那里已经有一个分支,但这只是一个注释,仅此而已。
When I delete the branch 'feature', nothing changes except that commit 523e2ac
is no longer labeled with "(feature)":
当我删除分支“功能”时,除了提交523e2ac
不再标有“(功能)”之外,没有任何变化:
* 9eb6e93 (HEAD, master) Merge branch 'feature'
|\
| * 523e2ac tres
| * 6d3cc0f dos
| * 1bc0b2e uno
* | d39734b three
* | 779d37b two
* | facbcbf one
|/
* 58848f4 Initial commit.
So, to answer your question, no, once you've deleted a branch, you cannot get git log
to annotate a commit with that branch name (because it doesn't exist anymore). However, you have some alternatives:
所以,要回答你的问题,不,一旦你删除了一个分支,你就不git log
能用那个分支名称来注释提交(因为它不再存在)。但是,您有一些选择:
Don't delete the branch. There's no harm in leaving branches around, except that it clutters up your screen when you type
git branch
. Also, you may want to re-use branch names, which could cause problems later on if you don't delete your branches.Tag the commit before you delete the branch. A tag is really a branch that doesn't move. You can even make the tag name the same as the branch name.
Satisfy yourself with the automatic commenting of merge commits. As mentioned before, when Git does a merge, by default, it references the name of the branch being merged in in the commit comment, creating a record that the branch existed. To me, this is the cleanest solution, based on the way branches work in Git. Since a branch doesn't really refer to a series of commits, it's really only of historical consequence that a branch existed.
不要删除分支。留下树枝并没有什么坏处,只是它会在您键入时弄乱您的屏幕
git branch
。此外,您可能希望重新使用分支名称,如果您不删除分支,这可能会在以后导致问题。在删除分支之前标记提交。标签实际上是一个不动的分支。您甚至可以使标签名称与分支名称相同。
让您对合并提交的自动评论感到满意。如前所述,当 Git 进行合并时,默认情况下,它会在提交注释中引用要合并的分支的名称,从而创建该分支存在的记录。对我来说,这是最干净的解决方案,基于 Git 中分支的工作方式。由于分支并不真正指一系列提交,因此分支存在实际上只是历史后果。
The other place that branch history may linger is your reflog, which simply logs what branches you're switching to/from. It's mostly there for disaster recovery (ooops, I didn't mean to delete that branch!), and it's not really useful for the kind of branch history you're talking about.
分支历史记录可能停留的另一个地方是您的 reflog,它只记录您切换到/从哪些分支。它主要用于灾难恢复(哎呀,我不是要删除该分支!),并且对于您正在谈论的那种分支历史记录并不是很有用。
回答by ellotheth
The comment is talking about merge commit messages, e.g. Merge QueueExample into master
. When you delete a branch, the branch is gone. If you want to maintain a topical view of your log with --decorate
, try using tags instead (or don't delete the branch).
该评论正在谈论合并提交消息,例如Merge QueueExample into master
. 当您删除一个分支时,该分支就消失了。如果您想使用 维护日志的主题视图,请--decorate
尝试改用标签(或不要删除分支)。
回答by rbelow
Use git log --merges
to list merge commits. The names of the deleted (but merged) branches will displayalong the names of the other merged branches.
使用git log --merges
到列表合并的提交。已删除(但已合并)的分支的名称将与其他合并分支的名称一起显示。
回答by twalberg
If you delete a branch in your local repo, it still exists in any other clones of that repo. If you push that deletion out to other repos, the branch itself (simply a conveniently named pointer to a specific commit) may cease to exist. However, the commit it pointed to will still be there for a while, even if absolutely nothing points to it. If it's been merged into other branches so that that commit forms the part of the ancestry of some other branch, then it will remain, as long as it's still reachable from an existing branch. Tracking it down, however, is a bit trickier. Early on, it'll be in your HEAD reflog for a while, but eventually it may disappear from there.
如果您删除本地存储库中的一个分支,它仍然存在于该存储库的任何其他克隆中。如果您将该删除推送到其他存储库,则分支本身(只是一个方便命名的指向特定提交的指针)可能不复存在。然而,它指向的提交仍然会存在一段时间,即使绝对没有任何指向它。如果它已被合并到其他分支,以便该提交构成某个其他分支的祖先的一部分,那么只要它仍然可以从现有分支访问,它就会保留下来。然而,追踪它有点棘手。早期,它会在你的 HEAD reflog 中存在一段时间,但最终它可能会从那里消失。
If you want them to always appear in git log
output, then don't delete them. Or, replace branch pointers with tags to point at where the branch was before you removed it.
如果您希望它们始终出现在git log
输出中,请不要删除它们。或者,将分支指针替换为标记,以指向删除分支之前的位置。