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

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


当前回答

这可能是太少也太迟了,但帮助我的是听起来很酷的“核”选项。基本上,使用命令filter-branch,你可以在整个git历史记录中删除文件或更改大量文件。

这里最好解释一下。

其他回答

从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

例如,如果你想删除最近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:

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

这可能是太少也太迟了,但帮助我的是听起来很酷的“核”选项。基本上,使用命令filter-branch,你可以在整个git历史记录中删除文件或更改大量文件。

这里最好解释一下。

我喜欢用rebase来做这个。 下面的n是最后的n次提交。如果你想删除第三个,用3替换n。

git rebase -i HEAD~n

然后,在清单中找到所需的提交,并将其从“pick”更改为“drop”。退出rebase并使用git push和“-f”选项,就像你刚刚做了一个rebase一样。

TL:博士;

n . git switch -C branch_name origin/branch_name~ Git push—force

完成后,远程分支将被n次提交还原。


解释:

使用git开关,按n次提交重置分支。c选项将强制创建具有相同名称的新分支。 将branch_name替换为分支名称, 将n(在命令的末尾)替换为要恢复的提交数。 命令1:git switch -C branch_name origin/branch_name~n 例如:git switch -C feature/dashboard origin/feature/dashboard~1 //返回dashboard分支的1次提交。 强制推送局部更改 命令#2:git push——force


提示:要撤消已提交(未推送)的更改,git将重置HEAD~