我知道这是在改写历史,很糟糕。

但是如何从远程分支永久删除少数提交?


当前回答

例如,如果你想删除最近3次提交,运行以下命令从文件系统(工作树)中删除更改,并在本地分支上提交历史(索引):

git reset --hard HEAD~3

然后运行以下命令(在您的本地机器上)强制远程分支重写其历史记录:

git push --force

恭喜你!全部完成!

一些注意事项:

您可以通过运行命令检索所需的提交id

git log

然后你可以像这样用<desired-commit-id>替换HEAD~N:

git reset --hard <desired-commit-id>

If you want to keep changes on file system and just modify index (commit history), use --soft flag like git reset --soft HEAD~3. Then you have chance to check your latest changes and keep or drop all or parts of them. In the latter case runnig git status shows the files changed since <desired-commit-id>. If you use --hard option, git status will tell you that your local branch is exactly the same as the remote one. If you don't use --hard nor --soft, the default mode is used that is --mixed. In this mode, git help reset says:

重置索引,但不重置工作树(也就是说,更改的文件是 保存但未标记为提交)并报告未保存的内容 更新。

其他回答

重要提示:确保你在“git push -f”上指定了哪些分支,否则你可能会无意中修改其他分支!(*)

本教程中显示了三个选项。以防链接中断,我将把主要步骤留在这里。

恢复完整的提交 删除最后一次提交 从列表中删除提交

1恢复完整提交

git revert dd61ab23

2删除上次提交

git push <<remote>> +dd61ab23^:<<BRANCH_NAME_HERE>>

或者,如果分支在本地可用

git reset HEAD^ --hard
git push <<remote>> -f

在+ dd61…是你的提交哈希,git将x^解释为x的父节点,+解释为强制的非快进推。

3从列表中删除提交

git rebase -i dd61ab23^

这将打开一个编辑器,显示所有提交的列表。删除你想要删除的那个。完成rebase和推力回购。

git rebase --continue
git push <remote_repo> <remote_branch> -f
 git reset --soft commit_id
 git stash save "message"
 git reset --hard commit_id
 git stash apply stash stash@{0}
 git push --force

例如,如果你想删除最近3次提交,运行以下命令从文件系统(工作树)中删除更改,并在本地分支上提交历史(索引):

git reset --hard HEAD~3

然后运行以下命令(在您的本地机器上)强制远程分支重写其历史记录:

git push --force

恭喜你!全部完成!

一些注意事项:

您可以通过运行命令检索所需的提交id

git log

然后你可以像这样用<desired-commit-id>替换HEAD~N:

git reset --hard <desired-commit-id>

If you want to keep changes on file system and just modify index (commit history), use --soft flag like git reset --soft HEAD~3. Then you have chance to check your latest changes and keep or drop all or parts of them. In the latter case runnig git status shows the files changed since <desired-commit-id>. If you use --hard option, git status will tell you that your local branch is exactly the same as the remote one. If you don't use --hard nor --soft, the default mode is used that is --mixed. In this mode, git help reset says:

重置索引,但不重置工作树(也就是说,更改的文件是 保存但未标记为提交)并报告未保存的内容 更新。

从pctroll的答案简化,类似的基于这篇博客文章。

# look up the commit id in git log or on github, e.g. 42480f3, then do
git checkout master
git checkout your_branch
git revert 42480f3
# a text editor will open, close it with ctrl+x (editor dependent)
git push origin your_branch
# or replace origin with your remote

有时,修复这个问题最简单的方法是从您知道代码良好的地方创建一个新的分支。然后,您可以不去管错误的分支历史记录,以防稍后需要从中挑选其他提交。这也确保您不会丢失任何提交历史记录。

来自你当地的错误分支:

git log

复制你想要分支所在的提交哈希并退出git日志

git checkout theHashYouJustCopied
git checkout -b your_new_awesome_branch

现在你有了一个你想要的新分支。

如果你还需要从错误的分支中保留一个特定的提交,而它不在你的新分支上,你可以选择你需要的特定提交:

git checkout the_errant_branch
git log

复制你需要拉入好的分支的提交哈希并退出git日志。

git checkout your_new_awesome_branch
git cherry-pick theHashYouJustCopied

拍拍自己的背。