如何永久撤消来自 git 远程的最近提交但将其保留在我的本地?

How to permanently undo recent commits from git remote but keep it in my local?

我用一个远程和一个本地创建了一个 git 仓库(只有一个主分支)。没有其他用户克隆过它,但远程路径可以被少数用户克隆。

我的本地克隆在提交 #17,我已经将提交 #12 推送到远程。我已经意识到,#6 之后的每一次提交都不应该被共享,远程需要(现在)保持在#6。

我不想丢失所有提交 1-17 和历史记录,但为了重置遥控器,我的理解是我必须首先将本地重置为 #6 并按下 -f。我是否可以将遥控器重置为 #6,同时在本地保持领先 17,这样如果有人克隆遥控器,他们就看不到易受攻击的提交?

我的想法是,我需要先将我的本地克隆到另一个本地,以便第二个本地在执行重置和 push -f 之前保留所有 17 次提交和历史记录。这是处理这种情况的方式吗?

我建议从本地 master 创建一个新的功能分支,以记录最多 #17 的提交。然后,硬重置本地 master 并强制推送:

# from local master
git branch feature
# now hard reset to commit #6
git reset --hard HEAD~10
# force push
git push --force origin master

现在本地 feature 分支代表您的 master 分支之前的状态,直到提交 #17,而本地 master 处于提交 #6,并且同步使用遥控器 master.

所以你想推送另一个分支,但是在你的本地(私有)repo 上有远程提交。

  1. 然后使用此远程状态创建新分支以获取此分支的本地副本git branch <branch_name>
  2. 检查您推送的本地分支,通过重置或变基将历史返回到您想要的点 git reset --hard
  3. 用强制标志推送 git force --push

The part I’m not sure of is that the new local git clone probably still has the same remote in its remotes - would it be invalid to then push to the remote from there?

我不确定你在问什么,但是当你将某些东西推送到远程时,那么在你的克隆之后应该有这个远程 - 这很正常。

对了,记住:

Do not rebase commits that exist outside your repository and people may have based work on them. If you follow that guideline, you’ll be fine. If you don’t, people will hate you, and you’ll be scorned by friends and family.