git删除旧的提交 [英] git remove old commits

查看:133
本文介绍了git删除旧的提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我犯了一个愚蠢的错误,不小心将node_modules文件夹提交到本地git,然后将其推送到github。这是一个巨大的文件夹,其他下载我的仓库的人也将在旧提交中下载此文件夹。我一直在尝试使用 rebase --onto rebase -i 删除提交,但是没有运气。这就是我的 git日志的样子。

I made a dumb error and accidentally committed a node_modules folder to my local git AND then pushed it to github. This is a huge folder and any else who downloads my repo will have also download this folder in the old commits. I've been trying to remove the commits with rebase --onto and rebase -i without luck. This is what my git log looks like.

$ git log --oneline
44549c5f (HEAD -> alex/matUI, origin/alex/matUI) fighting with gitignore
a5a5a79c changed ui to material   ##<---- remove me!
dbec4ab3 converting to material ui      ##<---- remove me!
cd4352f6 (origin/master, origin/HEAD, master) Merge pull request #1 from notsmart/addFullstack
a058bf1e moved files to new repo
80c82607 Added README.md

您如何删除这些提交?

推荐答案

您必须做两件事:


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

这篇关于git删除旧的提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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