Git:删除较早的提交但保留最近的更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17673771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 08:46:52  来源:igfitidea点击:

Git: remove earlier commit but keep recent changes

git

提问by Strernd

I got some commits in my repository:

我在我的存储库中有一些提交:

like:

喜欢:

A - Added Feature A
B - Removed Feature B
C - Redesigned Feature C
D - Added Feature D
E - Added Feature D

Where E is the most recent commit I made. Now I want to get rid of the changes I made with feature C but I want to keep the changes added with D and E.

其中 E 是我最近提交的。现在我想摆脱我对功能 C 所做的更改,但我想保留使用 D 和 E 添加的更改。

I hope you can help me.

我希望你能帮助我。

Thank you

谢谢

采纳答案by aib

If you only want to "get rid of the changes" (as specified in your question body) as opposed to actually "removing a commit" (as specified in the title), an easy option is addinga new commit, doing exactly the opposite of what the earlier commit did.

如果您只想“删除更改”(如您的问题正文中所指定)而不是实际“删除提交”(如标题中所指定),一个简单的选择是添加一个新提交,完全相反较早的提交做了什么。

It's not foolproof and it can result in conflicts due to changes done since, but it doesn't alter history, allows you to document the reversal and its reasons, and plays well with other working copies.

它不是万无一失的,它可能会因之后所做的更改而导致冲突,但它不会改变历史记录,允许您记录逆转及其原因,并且可以与其他工作副本很好地配合。

git revertis the tool used to make such evil twins of a set of commits.

git revert是用来制作一组提交的这种邪恶双胞胎的工具。

回答by BenLanc

Interactive rebaseis your friend!

交互式变基是您的朋友!

As others have said:

正如其他人所说:

$ git rebase -i HEAD~5

...where -iis the interactive flag, and HEAD~5means include the last 5 commits in the interactive rebase.

...-i交互式标志在哪里,HEAD~5表示包括交互式变基中的最后 5 次提交。

When you get editor up as a result of issuing the above, take a look at the comment in the opened file:

当您由于发出上述内容而启动编辑器时,请查看打开文件中的注释:

# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

The key bit for you is If you remove a line here THAT COMMIT WILL BE LOST.

对您来说,关键是如果您在此处删除一行,则提交将丢失。

So, delete the lines that reference the commits you want to be rid of, save and close, and git will handle the rest (you may have to fix some conflicts depending on the nature of the commit and the revert you're trying to delete).

因此,删除引用您想要删除的提交的行,保存并关闭,git 将处理其余部分(您可能需要根据提交的性质和您尝试删除的还原来修复一些冲突) )。

It's important to note that if you have already pushed your code and others have pulled it, the above will not work as the commits will find their way back in there the next time someone who has your branch checked out does a push. The interactive rebase deletes the commits to the extent that there is no record of them, so the other clones do not know they have been deleted. Next time they push, they'll try and re-instate them as the local clones "see" that the origin does not have the objects (commits) that you have deleted.

重要的是要注意,如果您已经推送了您的代码而其他人已经拉取了它,则上述内容将不起作用,因为提交将在下一次检出您的分支的人进行推送时找到回到那里的方式。交互式 rebase 将提交删除到没有记录的程度,因此其他克隆不知道它们已被删除。下次他们推送时,他们将尝试重新设置它们,因为本地克隆“看到”源没有您已删除的对象(提交)。

回答by mnagel

this is called rebasing: you want the -iswitch.

这称为变基:您想要-i切换。

read: https://www.atlassian.com/git/tutorial/rewriting-git-history#!rebase-i

阅读:https: //www.atlassian.com/git/tutorial/rewriting-git-history#!rebase-i

for a similar question/answer see: https://stackoverflow.com/a/2938393/2536029

有关类似的问题/答案,请参阅:https: //stackoverflow.com/a/2938393/2536029

回答by elhadi dp ?p????

to do that, follow these steps:

为此,请按照下列步骤操作:

git rebase -i HEAD~3

then move commit C to be the first at the bottom, you will have

然后将提交 C 移至底部的第一个,您将拥有

D - Added Feature D
E - Added Feature D
C - Redesigned Feature C

you save and exit, then

你保存并退出,然后

git reset HEAD^

this will undo commit C.

这将撤消提交 C。

回答by Andrew Aylett

If you only have changes locally, you're probably best off following a rebasing answer. If you've published the changes anywhere then you don't want to rebase because that will change history, meaning that everyone else will also have to rebase or you'll get your offending commit merged back in again.

如果您只在本地进行更改,那么您最好遵循重新定位的答案。如果你已经在任何地方发布了更改,那么你不想 rebase,因为这会改变历史,这意味着其他人也必须 rebase 或者你会再次合并你的违规提交。

If other people have your commits, the cleanest thing to do is to revert the offending change, not to excise it altogether. Use git revert COMMIT_IDto create a new commit that undoes the changes you applied in COMMIT_ID.

如果其他人有你的提交,最干净的做法是恢复有问题的更改,而不是完全删除它。使用git revert COMMIT_ID创建一个新的承诺是撤销你应用的变化COMMIT_ID

回答by elhadi dp ?p????

git reabse -i HEAD~3

you will have the list of your 3 last commit messages

您将拥有 3 条最后提交消息的列表

C - Redesigned Feature C
D - Added Feature D
E - Added Feature D

you just insert x git reset HEAD^ before the commit you want to undo, you want to undo commit C, so it will be like this

你只是在你想撤销的提交之前插入 x git reset HEAD^ ,你想撤销提交 C,所以它会是这样的

    C - Redesigned Feature C
x git reset HEAD^
    D - Added Feature D
    E - Added Feature D

exit rebase console, you will see that your commit is no more in the log and their files are staged.

退出 rebase 控制台,您将看到您的提交不再在日志中,并且它们的文件已暂存。

回答by Aminah Nuraini

If you use TortoiseGit in Windows, you can right-click on the Git folder then click TortoiseGit > Show log. You can view all of the commits done before and then check the commit you'd want to revert to.

如果您在 Windows 中使用 TortoiseGit,您可以右键单击 Git 文件夹,然后单击TortoiseGit > Show log。您可以查看之前完成的所有提交,然后检查您想要恢复的提交。

Click on the commit and you can see the changed files below. Click right on the files to be reverted and then click revert to parent revision.

单击提交,您可以在下面看到更改的文件。在要还原的文件上单击右键,然后单击revert to parent revision

How to revert commits

如何恢复提交

回答by Tyler S. Loeper

Use git revert

使用 git 还原

By far the best solution is to use git revert. This will make a new commit that undoes the changes in a specific SHA. This is a very safe way to remove a commit, and can be easily merge with other or remote branches.

到目前为止,最好的解决方案是使用 git revert。这将进行新的提交,以撤消特定 SHA 中的更改。这是删除提交的一种非常安全的方法,并且可以轻松地与其他或远程分支合并。

How to use Git Revert

如何使用 Git Revert

git revert <insert bad commit hash here>