如何从Git删除特定的提交? [英] How to remove specific commits from Git?

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

问题描述

我想从我的远程存储库中删除一些提交.我在回购协议中几乎没有这样的提交:

I want to remove few commits from my remote repository. I have few commits like this in my repo:

commits    messages

abcd123    some message
xyze456    another commit message
98y65r4    this commit is not required
987xcrt    this commit is not required
bl8976t    initial commit

我想从我的仓库中删除提交98y65r4987xcrt.如何实现?

I want to remove commit 98y65r4 and 987xcrt from my repo. How to achieve it?

推荐答案

对此有两种选择:安全的方法会给您留下肮脏的git历史记录,不安全的方法会给您留下清晰的git历史记录.您选择:

There are two alternatives to this: the safe one that leaves you with a dirty git history, or the unsafe one that leaves you with a clean git history. You pick:

您可以告诉git还原提交".这意味着它将引入一个更改,该更改将还原您在一次提交中所做的每个更改.您将需要执行两次(每次提交一次):

You can tell git to "Revert a commit". This means it will introduce a change that reverts each change you made in a commit. You would need to execute it twice (once per commit):

git revert 98y65r4
git revert 987xcrt

此解决方案将使您的git历史记录保持不变(您可以执行gitk --all来查看回购状态的图形表示):

This solution will leave your git history like this (you can execute gitk --all to see a graphical representation of the state of your repo):

2222222    revert of 987xcrt: this commit is not required
1111111    revert of 98y65r4: this commit is not required
abcd123    some message
xyze456    another commit message
98y65r4    this commit is not required
987xcrt    this commit is not required
bl8976t    initial commit

然后,您可以将新的2次提交推送到您的远程仓库中:

Then you can push the new 2 commits to your remote repo:

git push

此解决方案是安全的,因为它不会对远程存储库进行破坏性操作.

This solution is safe because it does not make destructive operations on your remote repo.

您还可以为此使用交互式资源库.命令是:

You also can use an interactive rebase for that. The command is:

git rebase -i bl8976t

在其中,您告诉git让您选择要混合在一起,重新排序或删除的提交.

In it, you are telling git to let you select which commits you want to mix together, reorder or remove.

执行命令时,将打开一个编辑器,其内容类似于以下内容:

When you execute the command an editor will open with a text similar to this:

pick    bl8976t    initial commit
pick    987xcrt    this commit is not required
pick    98y65r4    this commit is not required
pick    xyze456    another commit message
pick    abcd123    some message

只需继续删除不需要的行,就像这样:

Just go on and delete the lines you don't want, like this:

pick    bl8976t    initial commit
pick    xyze456    another commit message
pick    abcd123    some message

保存文件并关闭编辑器.

Save the file and close the editor.

到目前为止,这仅修改了存储库的本地副本(您可以使用gitk --all查看提交树).

So far, this has only modified the local copy of your repository (you can see the tree of commits with gitk --all).

现在您需要使用推力"将更改推到存储库中,但是在执行命令之前,请记住推力是一种破坏性操作,它将覆盖历史记录您的远程存储库,可能会给其他使用该存储库的人造成合并麻烦.如果您还可以并要执行推力,则命令为:

Now you need to push your changes to your repo, which is done with a "push force", but before you execute the command bear in mind that push force is a destructive operation, it will overwrite the history of your remote repository and can cause merge troubles to other people working on it. If you are ok and want to do the push force, the command is:

git push -f

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

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