如何获取(已删除)分支的git日志显示名称 [英] how to get git log display name of (deleted) branches

查看:335
本文介绍了如何获取(已删除)分支的git日志显示名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢查看git日志的方式

git log --graph --oneline --all --decorate

在我发现其输出有用的其他东西中,还有分支名称.但是,如果我删除了一个分支,则上面的内容将不再显示它们.我的意思是看到一堆东西,例如:

* 87c3294 (QueueExample) blah blah

比一堆

更具表现力(尤其是当列表变长时)

* 87c3294 blah blah

此问题的答案,尤其是 >

如何将它们打印在git log的输出中或至少以其他方式打印?

或者,我如何从git branch的输出中删除分支,同时仍然出于git log的目的而保留它们呢?

解决方案

在Git中,分支只是指向提交的指针,该指针随着在该分支上添加新提交而移动.换句话说,一旦指针移动,就不会再有以前的提交在该分支上的内存了.首先,这对我来说是一个很难的概念.也许是这样的名字:分支"让我想到了由边连接的多个节点,但是在Git中,分支实际上只是指向节点的移动指针.

git log会使用指向其的任何分支来忠实地注释提交.例如,我创建了一个仓库,在分支母版上提交了一个",两个"和三个",并在分支特征上提交了"uno","dos"和"tres",然后将特征合并回了母版中.这是git log在删除分支之前告诉我的内容:

*   9eb6e93 (HEAD, master) Merge branch 'feature'
|\
| * 523e2ac (feature) tres
| * 6d3cc0f dos
| * 1bc0b2e uno
* | d39734b three
* | 779d37b two
* | facbcbf one
|/
* 58848f4 Initial commit.

很容易被愚弄,以为(功能)"注释某种程度上是指右边的那个分支,但事实并非如此:只是指提交523e2ac.

请注意,默认情况下,当Git创建合并提交(在我们的示例中为9eb6e93)时,它会自动添加注释,说明其正在合并分支功能",因此有一些记录表明存在分支,但这只是一条评论,仅此而已.

当我删除分支功能"时,除了提交523e2ac不再标记为(功能)"以外,没有任何变化:

*   9eb6e93 (HEAD, master) Merge branch 'feature'
|\
| * 523e2ac tres
| * 6d3cc0f dos
| * 1bc0b2e uno
* | d39734b three
* | 779d37b two
* | facbcbf one
|/
* 58848f4 Initial commit.

因此,要回答您的问题,不,一旦删除了分支,就无法获得git log来用该分支名称注释提交(因为它不再存在).但是,您还有其他选择:

  • 不删除分支.留出分支没有什么害处,除了在您键入git branch时它会使屏幕杂乱无章.另外,您可能想重新使用分支名称,如果不删除分支,以后可能会引起问题.

  • 在删除分支之前标记提交.标签实际上是不会移动的分支.您甚至可以使标签名称与分支名称相同.

  • 对合并提交的自动注释感到满意.如前所述,默认情况下,当Git进行合并时,它会在提交注释中引用要合并的分支的名称,从而创建该分支存在的记录.对我来说,这是最干净的解决方案,它基于分支在Git中的工作方式.由于分支实际上并没有涉及到一系列提交,因此分支的存在实际上只是历史上的结果.

分支历史记录可能会持续存在的另一个地方是您的reflog,它仅记录您要切换到/从哪个分支转移的日志.它主要用于灾难恢复(糟糕的是,我并不是要删除该分支!),对于您正在谈论的那种分支历史来说,它并不是真的有用.

The way I like to see my git logs are with

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 question and in particular this comment seems 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?

Alternatively, how can I remove branches from the output of git branch, while still keeping them around for purpose of git log?

解决方案

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 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:

*   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.

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.

When I delete the branch 'feature', nothing changes except that commit 523e2ac is no longer labeled with "(feature)":

*   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:

  • 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.

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.

这篇关于如何获取(已删除)分支的git日志显示名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆