如何永久删除远程分支的一些提交

我知道这是历史的重写,这是不好的yada yada。

但是如何永久删除远程分支的一些提交?

你' git reset '你的本地分支,你git push --force修改本地分支到远程。 ( 这里的其他解决scheme ,包括删除远程分支,并重新推送它)

这个答案说明了这样一个命令的危险性,特别是如果人们依靠远程历史来获得自己的本地回购。
你需要做好准备,指出人们从 git rebase手册页的RECOVERING FROM UPSTREAM REBASE部分

重要:请确保您指定“git push -f”上的哪个分支,否则您可能会无意中修改其他分支! [*]

本教程中显示了三个选项。 万一链路断了,我会离开这里的主要步骤。

  1. 恢复完整的提交
  2. 删除最后一个提交
  3. 从列表中删除提交

1恢复完全提交

 git revert dd61ab23 

2删除最后一个提交

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

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

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

其中+ dd61 …是您的提交散列,git将x ^解释为x的父亲,并将+作为强制非fastforwared推送。

3从列表中删除提交

 git rebase -i dd61ab23^ 

这将打开并编辑器显示所有提交的列表。 删除一个你想摆脱的。 完成rebase并推动回购。

 git rebase --continue git push <remote_repo> <remote_branch> -f 

只要注意在还原非工作提交时使用last_working_commit_id

 git reset --hard <last_working_commit_id> 

所以我们不能重置为我们不想要的commit_id

那么确定,我们必须推送到远程分支:

 git push --force 

这可能太晚了,但是帮助我的是“核”选项。 基本上使用命令filter-branch你可以在整个GIT历史中移除文件或者改变大量文件。

这里最好解释一下 。

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

 # look up the commit id in git log or on github, eg 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 

有时候解决这个问题最简单的方法就是从你认识代码的地方开始build立一个新的分支。 那么你可以单独留下错误的分支历史logging,以备日后从其中挑选其他提交。 这也确保您不会丢失任何提交历史logging。

从你当地的错误分支:

 git log 

复制你想要的分支的提交散列并退出git日志

 git checkout theHashYouJustCopied git checkout -b your_new_awesome_branch 

现在你有一个新的分支,只是你想要的方式。

如果您还需要保持不在新分支上的错误分支的特定提交,则可以select您需要的特定提交:

 git checkout the_errant_branch git log 

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

 git checkout your_new_awesome_branch git cherry-pick theHashYouJustCopied 

轻拍自己的背部。