简体   繁体   English

如何在 git 日志中有标签时删除提交

[英]How can I remove commit in git log when it has tag

I want to remove the commit and clear up my git log history.我想删除提交并清除我的 git 日志历史记录。

But when I did, some commits was not disappeared.但是当我这样做时,一些提交并没有消失。

(using command: git log --all --graph --oneline) (使用命令:git log --all --graph --oneline)

I don't know why我不知道为什么

Here my example这是我的例子

git log_1 git log_1

I made 2 branches(br1, br2), and did not merge it with master.我做了2个分支(br1,br2),并没有将它与master合并。

and then I deleted 2 branches (br1, br2)然后我删除了 2 个分支(br1,br2)

But still there are commits in the log('log --all' not 'log')但是日志中仍然有提交('log --all' 不是 'log')

and also I did reset or rebase to delete them.而且我确实重置或重新设置以删除它们。

git log_2 git log_2

Did I something do wrong?我是不是做错了什么?

or Can't I remove the commit in log when it has tag?或者当它有标签时我不能删除日志中的提交吗?

The correct explanation is that you can't remove a commit at all .正确的解释是您根本无法删除提交。 It doesn't matter whether the commit has a tag or a branch name associated with it, as far as its existence goes.就提交是否存在而言,提交是否具有与之关联的标签或分支名称并不重要。 And it does not matter whether a commit exists either!提交是否存在也无关紧要! What matters to git log is whether git log can find the commit . git log 重要的是git log git log是否可以找到 commit

Obviously git log is not going to find a commit that doesn't exist.显然git log不会找到存在的提交。 But sometimes git log cannot find a commit that does exist.但有时git log找不到确实存在的提交。 So the interesting question is really: When can git log find a commit?所以有趣的问题真的是: git log什么时候可以找到提交?

The answer is: Whenever you give it the right hash ID, either directly or indirectly.答案是:只要你直接或间接地给它正确的 hash ID。

You give git log a commit hash ID directly by typing it in:你给git log一个 commit hash ID 直接输入:

git log b30a5cf

for instance.例如。 Git looks up that hash ID—technically this is a shortened hash ID; Git 查找 hash ID — 从技术上讲,这是一个缩短的 hash ID; Git checks to make sure that there is exactly one commit with the full hash ID that starts with this b30a5cf number—and having found that commit, git log shows that commit. Git 检查以确保恰好有一个提交具有完整的 hash ID 以该b30a5cf编号开头,并且在找到该提交后, git log显示该提交。 Then it goes on to show some more commits.然后它继续显示更多的提交。

You give git log a commit hash ID indirectly by doing any of these:您通过执行以下任何操作间接git log提交 hash ID:

  • providing a name that translates to a hash ID, such as master or basePoint for instance;提供转换为 hash ID 的名称,例如masterbasePoint
  • providing a flag like --branches , --tags , or --all that tells git log to look up all branch names, all tag names, or all references ;提供像--branches--tags--all这样的标志,告诉git log查找所有分支名称、所有标签名称或所有引用 1 or 1
  • providing, directly or indirectly, the hash ID of a commit that in turn contains the hash ID of another commit, or perhaps multiple additional commits.直接或间接地提供一个提交的 hash ID,该 ID 又包含另一个提交的 hash ID,或者可能是多个额外的提交。

This last point is one of the keys to understanding Git.最后一点是理解 Git 的关键之一。 Each commit contains a list of previous commit hash IDs.每个提交都包含先前提交 hash ID 的列表。 In most commits, this list has just one entry, which Git calls the parent of the commit.在大多数提交中,此列表只有一个条目,Git 将其称为提交的项。 So each commit points—backwards, not forwards—to its parent:所以每个提交都指向它的父节点——向后,而不是向前:

... <-o <-o <-o ...

Here the round o s represent commits, with later commits towards the right.这里的round o s 代表提交,后面的提交向右。 The later commits each point backwards to one earlier commit.后者将每个点向后提交到一个较早的提交。

If you run:如果你运行:

git log master

or:或者:

git log b30a5cf

you tell git log to look up that particular commit, which it does;你告诉git log查找那个特定的提交,它确实这样做了; it shows the commit, and then finds the commit's parent(s), which in this case is just the one commit c348c14 .它显示提交,然后找到提交的父级,在这种情况下,它只是一个提交c348c14 The git log command then displays that commit, and follows the backwards arrow to the next—or rather, previous—commit and displays that one. git log命令然后显示提交,并按照向后箭头到下一个(或者更确切地说,上一个)提交并显示该提交。

With --all , you tell git log to start at all commits that have names.使用--all ,您可以告诉git log从所有具有名称的提交开始。 This gets more complicated, because now git log has to show multiple commits all at once, and it literally can't.这变得更加复杂,因为现在git log必须同时显示多个提交,而实际上不能。 So it picks out a commit to show based on a priority .因此,它根据优先级挑选出要显示的提交。 Using --graph modifies the priority selection so that git log can draw the connections from each commit to its predecessor(s).使用--graph修改优先级选择,以便git log可以绘制从每个提交到其前任的连接。

Deleting a name does not delete a commit.删除名称不会删除提交。 It merely removes your access to that commit through that name.它只是通过该名称删除您对该提交的访问。 If you have access to that commit through some other name, you will still be able to find the commit.如果您可以通过其他名称访问该提交,您仍然可以找到该提交。 If you memorize the commit's hash ID, then even with no names or other commits providing access, you can access the commit by raw hash ID.如果您记住了提交的 hash ID,那么即使没有名称或其他提交提供访问权限,您也可以通过原始 hash ID 访问提交。

If there are literally no names by which Git can ever find a commit, that commit becomes eligible for what Git calls garbage collection .如果字面上没有Git 可以找到提交的名称,则该提交将符合 Git 所谓的垃圾收集的条件。 These "abandoned" commits will persist for a minimum of 14 days by default, and 30 days by default for most cases.默认情况下,这些“被放弃”的提交将持续至少 14 天,大多数情况下默认持续 30 天。 After that, git gc may decide that the commit has no actual value, and remove it for real.之后, git gc可能会认为该提交没有实际价值,并将其真正删除。 But you can't remove it directly, and you have no control over precisely when git gc will get around to removing it.但是您不能直接删除它,并且您无法准确控制git gc何时开始删除它。 All you can do is remove the names that find a commit so that you no longer have to see it.您所能做的就是删除找到提交的名称,以便您不再需要查看它。 2 2


1 A reference is the generalized form of "any name that can find some Git object". 1引用是“可以找到某些 Git 对象的任何名称”的概括形式。 Branch and tag names are two kinds of references, but there are many more.分支和标签名称是两种引用,但还有更多。

2 In an emergency—such as after accidentally adding a terabyte database to a Git repository—it's possible to clean these things out manually. 2在紧急情况下(例如在意外将 TB 数据库添加到 Git 存储库后),可以手动清除这些内容。 It's rather tricky though.不过,这相当棘手。 It's not something you want to do often.这不是你想要经常做的事情。

git log will display any commit reachable . git log将显示任何可访问的提交。

Meaning:意义:

  • reachable from a branch HEAD可从分支 HEAD
  • or from a tag或来自标签

You would therefore need to delete the tag, not just the branch因此,您需要删除标签,而不仅仅是分支

git tag -d br2-tag

Note that all those steps are for your local repository only.请注意,所有这些步骤仅适用于您的本地存储库。
If you had also pushed those branches and tags, you would need to push their deletion too .如果您还推送了这些分支和标签,您也需要推送它们的删除

git push --delete origin br2
git push --delete origin br2-tag

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM