我知道重写历史是不好的,但如何永久地从远程分支中删除几个提交?
我知道重写历史是不好的,但如何永久地从远程分支中删除几个提交?
git reset --hard
重置本地分支以从工作树和索引中删除更改,然后使用git push --force
(或git push --force-with-lease
)将修改后的本地分支推送到远程。这个SO答案说明了这种命令的危险性,尤其是如果人们依赖于远程历史记录来管理自己的本地存储库。
您需要准备好指出人们到git rebase
手册的RECOVERING FROM UPSTREAM REBASE部分。
此外,正如ringo在评论中指出的那样,如果远程分支受到强制推送保护,那么像这个答案中所示的git revert
可能更可取。
在Git 2.23(2019年8月,九年后),您将使用新命令git switch
。
即:git switch -C mybranch origin/mybranch~n
(将n
替换为要删除的提交数)
这将恢复索引和工作树,就像git reset --hard
一样。
文档添加:
-C <new-branch> --force-create <new-branch>
Similar to
--create
except that if<new-branch>
already exists, it will be reset to<start-point>
.
This is a convenient shortcut for:$ git branch -f <new-branch> $ git switch <new-branch>
push --force
。 - VonCgit gc
并不总是经常运行。例如在 GitHub 上:https://twitter.com/githubhelp/status/387926738161774592?lang=es - VonC在回滚不可用的提交时,请注意使用last_working_commit_id
。
git reset --hard <last_working_commit_id>
所以我们不能将代码重置到我们不想要的 commit_id
。
然后,确保我们必须推送到远程分支:
git push --force
git reset --hard
应该做的事情。 - Luke在这篇教程中介绍了三种选项。如果链接失效了,我会在这里列出主要步骤。
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^
这将打开一个编辑器,显示所有提交的列表。删除您想要摆脱的那个提交。完成变基并强制推送到仓库。
git rebase --continue
git push <remote_repo> <remote_branch> -f
3
次提交,可以运行以下命令来从本地分支的文件系统(工作树)和提交历史(索引)中删除更改:git reset --hard HEAD~3
然后在您的本地计算机上运行以下命令,强制远程分支重写其历史记录:
git push --force
git log
然后,您可以像这样使用 <desired-commit-id>
替换 HEAD~N
:
git reset --hard <desired-commit-id>
--soft
标志,例如git reset --soft HEAD~3
。然后您有机会检查最新更改并保留或丢弃它们的全部或部分内容。在后一种情况下,运行git status
会显示自<desired-commit-id>
以来已更改的文件。如果使用了--hard
选项,则git status
将告诉您本地分支与远程分支完全相同。如果既不使用--hard
也不使用--soft
,则使用默认模式,即--mixed
。在此模式下,git help reset
说: 重置索引但不重置工作树(即保留更改的文件但不标记为提交)并报告未更新的内容。简述:
git switch -C branch_name origin/branch_name~n
:使用 git switch
命令,将分支还原到前 n
次提交的状态。选项 -C
强制创建一个同名的新分支。git push --force
:强制推送本地更改到远程仓库。执行完上述两个命令后,远程分支将被还原 n
次提交。
解释:
使用 git switch
命令,将分支还原到前 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 reset HEAD~
命令。
简化自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
可能为时已晚,但对我有帮助的是听起来很酷的“核”选项。基本上使用命令filter-branch
,您可以删除文件或在整个git历史记录中更改大量文件的某些内容。
最好的解释可以在这里找到。
filter-branch
,而是推荐使用 https://github.com/newren/git-filter-repo。 - TanguyP这是一种干净的方法,可以从远程仓库中删除您的提交记录,而不会丢失您的工作。
git reset --soft HEAD~1 # 1 represents only last 1 commit
git stash # hold your work temporary storage temporarily.
git pull # bring your local in sync with remote
git reset --hard HEAD~1 # hard reset 1 commit behind (deletes local commit)
git push -f # force push to sync local with remote
git stash pop # get back your unpushed work from stash
以下是我的情况发生了什么
我正在处理一个名为 redesign
的分支。
我不小心提交并推送了一个包含我的机密文件的工作。所以我的机密现在被暴露了(Gitguardian 在我的电子邮件中警告我 xD)。 现在我想从存储库中删除我的机密文件,但是想保留我的工作。
运行 git reset --soft HEAD~1
,这将使您的本地存储库在您的计算机上向后移动 1 个提交(修改数字以向后移动 n 个提交)。
现在您将看到您提交的文件为 未暂存
。
通过运行 git stash
将此工作暂时移动到存储区中,以便在本地保存。
现在通过运行 git pull
使您的本地与远程同步。
现在运行 git reset --hard HEAD~1
(再次修改数字以删除 n 个提交),以从您的存储库中删除远程提交,并进行强制推送 git push -f
。
您将看到来自远程存储库的提交已被删除。
现在通过运行 git stash pop
从存储区中获取您的工作。
现在对未推送的工作进行所需的更改。
git log
复制你想要分支所在的提交哈希并退出git log
git checkout theHashYouJustCopied
git checkout -b your_new_awesome_branch
git checkout the_errant_branch
git log
git checkout your_new_awesome_branch
git cherry-pick theHashYouJustCopied
给自己点个赞。
git reset --soft commit_id
git stash save "message"
git reset --hard commit_id
git stash apply stash stash@{0}
git push --force