简体   繁体   English

如何在历史记录中更改特定Commit中的文件?

[英]How do I change files in a specific Commit in my history?

say I have the following commit history : 说我有以下提交历史记录:

4317629 Bug-100 : Formatting / Cosmetic Changes
da10348 Bug-100 : Wire Up UI
9b49b0a Bug-100 : Added UI

Now, say I find that in the commit da10348 , I had a few files : a.txt , b.txt , c.txt that contain lines that I need to remove / fix. 现在,假设我发现在提交da10348 ,我有一些文件: a.txtb.txtc.txt ,其中包含我需要删除/修复的行。

Can someone tell me just how I can do that? 有人能告诉我我该怎么做吗?

You have to rebase your branch onto the commit you want to change in order to change the history after the commit so actually in this case you have to rebase your branch on the commit before the one you want to change( ^ operator): 您必须将您的分支重新绑定到要更改的提交,以便在提交后更改历史记录,因此实际上在这种情况下,您必须在要更改的那个之前在提交上重新分支您的分支( ^运算符):

git rebase -i da10348^

change the commit to edit and implement your changes and then: 更改提交以编辑和实施更改,然后:

git commit -a --amend --no-edit

and finally finish the rebase: 最后完成了变革:

git rebase --continue

Note that you can only do this if you're comfortable rewriting history for all descendant commits of the commit you want to change. 请注意,只有在您为要更改的提交的所有后代提交重写历史记录时,才能执行此操作。 Otherwise you should just make the change at the tip of your current master branch. 否则,您应该只在当前主分支的顶端进行更改。

If you do want to do it you can execute 如果你想这样做,你可以执行

git checkout da10348
# make changes
git commit -a --amend
git rebase --onto <amended-sha> da10348 <your-branch>

Or if you prefer interactive rebase 或者如果你喜欢交互式rebase

git rebase -i da10348^
# change "pick da10348" to "edit da10348"
# save, and make changes when rebase -i stops at da10348

This is the method I find the easiest for doing small fixes to recent commits, because it requires just one rebase command (ie no git rebase --continue ) and doesn't involve remembering commit hashes. 这是我发现最容易对最近的提交进行小修复的方法,因为它只需要一个rebase命令(即没有git rebase --continue ),并且不涉及记住提交哈希。

  1. Made a new commit with those fixes (typically I use just "fix" as the commit message) 使用这些修复程序进行了新的提交(通常我只使用“修复”作为提交消息)
  2. git rebase -i origin/master if the commit has not yet been pushed to origin, or alternatively git rebase -i HEAD~3 based on how far in the history the commit to change is (it's OK to overestimate - the older commits won't be changed) git rebase -i origin/master如果提交尚未被推送到源,或者git rebase -i HEAD~3基于历史记录中提交更改的位置(可以高估 - 旧提交赢了'改变)
  3. In the interactive rebase's editor, move the new commit with those fixes to be immediately after the commit you want to fix and change its command to f or fixup . 在交互式rebase的编辑器中,将带有这些修复的新提交移动到要修复的提交之后,并将其命令更改为ffixup
  4. Save and close the editor, wait for rebase to finish and check whether the resulting history is what you wanted. 保存并关闭编辑器,等待rebase完成并检查生成的历史记录是否符合您的要求。

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

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