简体   繁体   English

Git从历史记录中删除某些提交,但保留它们的更改

[英]Git remove certain commits from history, but keep their changes

Here is the scenario: 这是场景:

I have updated a PHP CMS to a new version. 我已经将PHP CMS更新为新版本。 In doing so, the guides recommend to keep the old files before replacing them with the new ones. 这样做时,指南建议保留旧文件,然后再用新文件替换。 Yes, this is dumb, because I could've time traveled using Git. 是的,这很愚蠢,因为我本来可以使用Git来旅行的。 I didn't. 我没有

What I did: 我做了什么:

  1. Rename certain folders with source files to "old_whatever". 将带有源文件的某些文件夹重命名为“ old_whatever”。 The folders together contain a few 1,000 files. 这些文件夹总共包含1,000个文件。
  2. Commit the changes. 提交更改。
  3. Paste the new folders. 粘贴新文件夹。 We now have two times a few 1,000 files. 现在,我们有1000个文件的两倍。
  4. Commit the changes. 提交更改。
  5. Update the CMS. 更新CMS。
  6. Delete the old folders. 删除旧文件夹。
  7. Commit the changes. 提交更改。

Bang. 砰。 Now, git has a few very large commits, because of all the files actually being duplicated. 现在,由于所有文件实际上都是重复的,因此git有一些非常大的提交。 I do not need those steps to be in the history. 我不需要那些步骤成为历史。 I do want to keep the current state of things, so: I want to keep the file structure exactly as it is under step 7. I want to dump steps 2 and 4. 我确实想保持当前状态,所以:我想保持文件结构与步骤7完全相同。我要转储步骤2和4。

Extra info: the difference in files between step 1 and step 7 is only about 1 or 2%, at the most. 额外信息:步骤1和步骤7之间的文件差异最多仅为1%或2%。 But because of the steps I took, Git thought all of those were new files, I think. 但是由于我采取的步骤,我认为Git认为所有这些都是新文件。

When I now try to push the commits to a remote, the changes to push are suddenly 3.54 GB, up from a couple hundred MB. 现在,当我尝试将提交推送到远程服务器时,要进行的更改突然从几百MB增加到3.54 GB。

What you want to achieve here is an interactive rebase. 您要在此处实现的是交互式基础。 You will tell Git "I want to rewrite my history between commit x and commit z". 您将告诉Git“我想重写我在提交x和提交z之间的历史记录”。 Then git will show you a text file, like a script you can edit to indicate what you want to do with each commit (update its message, delete it, merge with previous, etc.). 然后git将向您显示一个文本文件,就像一个脚本,您可以对其进行编辑以指示每次提交时要执行的操作(更新其消息,将其删除,与先前的合并等)。 Then, when you finished, save the file, and let the magic happen. 然后,完成操作后,保存文件,然后神奇地发生。

The base command is: 基本命令是:

git rebase -i

I strongly suggest to study how this command work, and maybe perform some tests on another repository to understand well. 我强烈建议研究此命令的工作方式,并可能在另一个存储库上执行一些测试以更好地理解。 When you will manage this command, you will understand how powerful it is. 当您管理此命令时,您将了解它的功能。 And this completely match you current need. 这完全符合您当前的需求。

The doc: https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History 该文档: https//git-scm.com/book/en/v2/Git-Tools-Rewriting-History

Good luck ;) 祝好运 ;)

Note: the action to "merge" some commits into 1 is named "Squashing Commits" 注意:将某些提交“合并”为1的动作称为“压缩提交”

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

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