git 如何从远程分支永久删除一些提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3293531/
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
How to permanently remove few commits from remote branch
提问by Arnis Lapsa
I know that's rewriting of history which is bad yada yada.
我知道那是对历史的改写,这是糟糕的 yada yada。
But how to permanently remove few commits from remote branch?
但是如何从远程分支永久删除一些提交?
采纳答案by VonC
You git reset --hard
your local branch to remove changes from working tree and index, and you git push --force
your revised local branch to the remote. (other solution here, involving deleting the remote branch, and re-pushing it)
您git reset --hard
将本地分支从工作树和索引中删除更改,并将git push --force
修改后的本地分支移至远程。(这里的其他解决方案,包括删除远程分支,并重新推送它)
This SO answerillustrates the danger of such a command, especially if people depends on the remote history for their own local repos.
You need to be prepared to point out people to the RECOVERING FROM UPSTREAM REBASEsection of the git rebase
man page
这个SO 答案说明了这样一个命令的危险,特别是如果人们依赖于他们自己的本地存储库的远程历史。
您需要准备好将人们指出到手册页的从上游 REBASE 恢复部分git rebase
With Git 2.23 (August 2019, nine years later), you would use the new command git switch
.
That is: git switch -C mybranch origin/mybranch~n
(replace n
by the number of commits to remove)
在 Git 2.23(2019 年 8 月,九年后)中,您将使用新命令git switch
。
即:(
替换为要删除的提交数)git switch -C mybranch origin/mybranch~n
n
That will restore the index and working tree, like a git reset --hard
would.
这将恢复索引和工作树,就像git reset --hard
这样。
回答by hd84335
Just note to use the last_working_commit_id
, when reverting a non-working commit
请注意last_working_commit_id
在恢复非工作提交时使用,
git reset --hard <last_working_commit_id>
So we must not reset to the commit_id
that we don't want.
所以我们一定不要重置为commit_id
我们不想要的。
Then sure, we must push to remote branch:
那么当然,我们必须推送到远程分支:
git push --force
回答by reefaktor
Important: Make sure you specify which branches on "git push -f" or you might inadvertently modify other branches![*]
重要提示:确保您在“git push -f”上指定了哪些分支,否则您可能会无意中修改其他分支![*]
There are three options shown in this tutorial. In case the link breaks I'll leave the main steps here.
本教程中显示了三个选项。如果链接断开,我将把主要步骤留在这里。
- Revert the full commit
- Delete the last commit
- Delete commit from a list
- 还原完整提交
- 删除最后一次提交
- 从列表中删除提交
1 Revert the full commit
1 恢复完整提交
git revert dd61ab23
2 Delete the last commit
2 删除最后一次提交
git push <<remote>> +dd61ab23^:<<BRANCH_NAME_HERE>>
or, if the branch is available locally
或者,如果分支机构在本地可用
git reset HEAD^ --hard
git push <<remote>> -f
where +dd61... is your commit hash and git interprets x^ as the parent of x, and + as a forced non-fastforwared push.
其中 +dd61... 是您的提交哈希,git 将 x^ 解释为 x 的父级,并将 + 解释为强制的非 fastforwared 推送。
3 Delete the commit from a list
3 从列表中删除提交
git rebase -i dd61ab23^
This will open and editor showing a list of all commits. Delete the one you want to get rid off. Finish the rebase and push force to repo.
这将打开并显示所有提交列表的编辑器。删除你想摆脱的那个。完成 rebase 并推动 repo。
git rebase --continue
git push <remote_repo> <remote_branch> -f
回答by Abdollah
If you want to delete for example the last 3
commits, run the following command to remove the changes from the file system (working tree) and commit history (index) on your local branch:
如果要删除例如最后一次3
提交,请运行以下命令以从本地分支上的文件系统(工作树)和提交历史记录(索引)中删除更改:
git reset --hard HEAD~3
Then run the following command (on your local machine) to force the remote branch to rewrite its history:
然后运行以下命令(在您的本地机器上)强制远程分支重写其历史记录:
git push --force
Congratulations! All DONE!
恭喜!全部完成!
Some notes:
一些注意事项:
You can retrieve the desired commit id by running
您可以通过运行来检索所需的提交 ID
git log
Then you can replace HEAD~N
with <desired-commit-id>
like this:
然后你就可以代替HEAD~N
使用<desired-commit-id>
这样的:
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:
如果您想保留对文件系统的更改并只修改索引(提交历史记录),请使用--soft
类似git reset --soft HEAD~3
. 然后您有机会检查您的最新更改并保留或删除它们的全部或部分。在后一种情况下,runniggit status
显示自<desired-commit-id>
. 如果您使用--hard
选项,git status
会告诉您您的本地分支与远程分支完全相同。如果您不使用--hard
nor --soft
,则使用默认模式即--mixed
. 在这种模式下,git help reset
说:
Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated.
重置索引但不重置工作树(即,更改的文件被保留但未标记为提交)并报告尚未更新的内容。
回答by jansmolders86
This might be too little too late but what helped me is the cool sounding 'nuclear' option. Basically using the command filter-branch
you can remove files or change something over a large number of files throughout your entire git history.
这可能为时已晚,但对我有帮助的是听起来很酷的“核”选项。基本上使用该命令,filter-branch
您可以删除文件或在整个 git 历史记录中对大量文件进行更改。
It is best explained here.
最好在这里解释。
回答by ted.strauss
Simplifying from pctroll's answer, similarly based on this blog post.
从 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
回答by Douglas.Sesar
Sometimes the easiest way to fix this issue is to make a new branch from the place where you know the code is good. Then you can leave the errant branch history alone in case you need to cherry-pick other commits from it later. This also ensures you did not lose any commit history.
有时,解决此问题的最简单方法是从您知道代码良好的地方创建一个新分支。然后,您可以单独保留错误的分支历史记录,以防以后需要从中挑选其他提交。这也确保您不会丢失任何提交历史记录。
From your local errant branch:
从您当地的错误分支:
git log
copy the commit hash that you wanted the branch to be at and exit the git log
复制您希望分支所在的提交哈希并退出 git 日志
git checkout theHashYouJustCopied
git checkout -b your_new_awesome_branch
Now you have a new branch just the way you want it.
现在你有了一个你想要的新分支。
If you also needed to keep a specific commit from the errant branch that is not on your new branch, you can just cherry-pick that specific commit you need:
如果您还需要保留不在新分支上的错误分支的特定提交,您可以只挑选您需要的特定提交:
git checkout the_errant_branch
git log
Copy the commit hash of the one commit you need to pull into the good branch and exit the git log.
复制您需要拉入良好分支并退出 git 日志的一次提交的提交哈希。
git checkout your_new_awesome_branch
git cherry-pick theHashYouJustCopied
Pat yourself on the back.
拍拍自己的后背。
回答by pandorago
git reset --soft commit_id
git stash save "message"
git reset --hard commit_id
git stash apply stash stash@{0}
git push --force