且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

git删除旧的提交

更新时间:2023-02-02 18:16:13

您必须做两件事:


  1. 在本地删除那些提交

  2. push它们会强行覆盖原点上的分支

编辑:实际上备份那些首先要删除的文件,因为此方法将删除

actually back up those files that will be removed first, because this method will remove them from your filesystem.

首先:


git rebase -i HEAD〜 4

git rebase -i HEAD~4

现在您有了一个开放式编辑器,其内容与您编写的内容相似。删除不需要的行。保存并退出编辑器。

Now you have an open editor with lines similar to what you wrote. Remove the lines with commits you don't want. Save and exit the editor.

检查 git log 是否正确。

然后:


git push -f

git push -f

说明:

首先,您开始了一个交互式历史记录编辑会话。在编辑器中,您下面有可能的选项,已被注释掉。您可以做很多事情,例如通过删除行来删除提交,将它们挤在一起,仅通过对行进行重新排序等来重新排序等。

First you started an interactive history edit session. You have possible options below in the editor, commented out. You can do many things like removing commits by removing lines, squashing them together, reordering by just reordering lines etc.

然后,您删除了提交行并保存了。发生的事情是git尝试创建新的提交链以应用所需的更改。实际上创建了新的提交(提交的一部分链接到先前的提交),因此对于已更改的提交,存在新的哈希(因为从技术上讲,它们是新的)。您将看到origin / alex / matUI不再位于您的HEAD上(在 git log 中)。

Then you removed the commit lines and saved. What happened is git tried to create new chain of commits to apply your desired changes. Actually new commits were created (part of the commit is link to previous one), so there are new hashes for commits that have been changed (because technically they are new ones). You will see that origin/alex/matUI is no longer on your HEAD (in git log).

最后你用力推。这将用您当前的alex / matUI覆盖origin / alex / matUI。实际上,这会覆盖您HEAD指向的任何分支,并与原点上的分支捆绑在一起(您的alex / matUI与origin / alex / matUI捆绑在一起,这并不神奇,这是您手动创建或创建的显式领带拉/克隆时)。通常 push 是保守的,只允许在分支提示后添加。 -f 会通过它。用力卢克:)

Finally you pushed with force. This overwrote origin/alex/matUI with your current alex/matUI. This actually overwrites any branch that your HEAD is pointing on and is tied with a branch on origin (your alex/matUI is tied with origin/alex/matUI, that's no magic, it's an explicit tie that you either create manually or have it created when pulling/cloning). Normally push is conservative, allows only additions after tips of your branches. -f forces through that. Use the force Luke :)