Git 删除特定提交后的远程历史记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22674267/
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-10 17:56:00  来源:igfitidea点击:

Git remove remote history past certain commit

gitgitlab

提问by Jonathan S. Fisher

I have a Git/Gitlab repository. We used to commit straight to master, but we decided to switch to using feature branches like the rest of the world for this release.

我有一个 Git/Gitlab 存储库。我们曾经直接提交给 master,但我们决定在这个版本中切换到像世界其他地方一样使用功能分支。

We need to reset our remote master to the state it was in immediately after the last release. If someone has already committed to the master directly, how can I reset it to a clean state, removing all history past the last release?

我们需要将我们的远程 master 重置为它在上次发布后立即所处的状态。如果有人已经直接提交给 master,我如何将其重置为干净状态,删除上次发布之后的所有历史记录?

I've spent about an hour googling now and can't find an answer to this specific question. Sorry if it seems redundant, it seems like such a simple task with no obvious answer!

我现在花了大约一个小时在谷歌上搜索,但找不到这个特定问题的答案。对不起,如果它看起来多余,它似乎是一个没有明显答案的简单任务!

回答by Paul Draper

To reset a local branch,

要重置本地分支,

git branch -f master last-release

To reset a remote branch,

要重置远程分支,

git push -f origin last-release:master

where last-releaseis the ref (commit id or branch) you want to reset masterto.

last-release您要重置master到的 ref(提交 ID 或分支)在哪里。

(Neither of these affect your working tree; you can even do these from a bare repo, if you wish.)

(这些都不会影响您的工作树;如果您愿意,您甚至可以从裸仓库中执行这些操作。)

回答by Jonathan S. Fisher

Sometimes, you post on Stack Overflow and you immediately figure it out a second later:

有时,您在 Stack Overflow 上发帖,一秒钟后您立即弄清楚:

$ git reset --hard HEAD~9
$ git push --all --force

Now delete your local repo, re-clone.

现在删除您的本地存储库,重新克隆。