git 如何将 github 存储库回滚到特定提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4372435/
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 can I rollback a github repository to a specific commit?
提问by Jonathan Vanasco
My github has 100 commits in it right now. I need to rollback the repository to commit 80, and remove all the subsequent ones.
我的 github 现在有 100 次提交。我需要回滚存储库以提交 80,并删除所有后续存储库。
Why? This repo is supposed to be for merging from miscellaneous users. A bunch of merges went in as commits from me, due to excessive editing. That was due to a mislabeling of my remote branches, where 3 developers were labeled as each other. I need to reset to that point, and then pull forwards.
为什么?这个 repo 应该用于从其他用户合并。由于过度编辑,我提交了一堆合并。这是由于我的远程分支的标签错误,其中 3 个开发人员被标记为彼此。我需要重置到那个点,然后向前拉。
I wanted to rebase, as in this example: How can I remove a commit on GitHub?
我想变基,如本例所示:如何删除 GitHub 上的提交?
However, git wants me to do a lot of conflict management. Is there a simpler way?
但是,git 要我做很多冲突管理。有没有更简单的方法?
回答by jtdubs
git reset --hard <old-commit-id>
git push -f <remote-name> <branch-name>
Note: As written in comments below, Using this is dangerous in a collaborative environment: you're rewriting history
注意:正如下面的评论中所写,在协作环境中使用它是危险的:您正在重写历史
回答by Nick Res
To undo the most recent commit I do this:
要撤消最近的提交,我这样做:
First:
第一的:
git log
get the very latest SHA id to undo.
获取要撤消的最新 SHA id。
git revert SHA
That will create a new commit that does the exact opposite of your commit. Then you can push this new commit to bring your app to the state it was before, and your git history will show these changes accordingly.
这将创建一个与您的提交完全相反的新提交。然后你可以推送这个新的提交,让你的应用恢复到之前的状态,你的 git 历史将相应地显示这些更改。
This is good for an immediate redo of something you just committed, which I find is more often the case for me.
这有利于立即重做您刚刚提交的内容,我发现这对我来说更常见。
As Mike metioned, you can also do this:
正如迈克提到的,你也可以这样做:
git revert HEAD
回答by CommaToast
Another way:
其它的办法:
Checkout the branch you want to revert, then reset your local working copy back to the commit that you want to be the latest one on the remote server (everything after it will go bye-bye). To do this, in SourceTree, I right-clicked on the and selected "Reset BRANCHNAME to this commit".
签出您想要恢复的分支,然后将您的本地工作副本重置回您希望成为远程服务器上最新一个的提交(之后的所有内容都将再见)。为此,在 SourceTree 中,我右键单击并选择了“将 BRANCHNAME 重置为此提交”。
Then navigate to your repository's local directory and run this command:
然后导航到存储库的本地目录并运行以下命令:
git -c diff.mnemonicprefix=false -c core.quotepath=false push -v -f -- tags REPOSITORY_NAME BRANCHNAME:BRANCHNAME
This will erase all commits after the current one in your local repository but only for that one branch.
这将删除本地存储库中当前提交之后的所有提交,但仅限于该分支。
回答by Ion Lesan
Most suggestions are assuming that you need to somehow destroy the last 20 commits, which is why it means "rewriting history", but you don't have to.
大多数建议都假设您需要以某种方式销毁最后 20 次提交,这就是为什么它意味着“重写历史”,但您不必这样做。
Just create a new branch from the commit #80 and work on that branch going forward. The other 20 commits will stay on the old orphaned branch.
只需从提交 #80 创建一个新分支,然后继续处理该分支。其他 20 个提交将保留在旧的孤立分支上。
If you absolutely want your new branch to have the same name, remember that branch are basically just labels. Just rename your old branch to something else, then create the new branch at commit #80 with the name you want.
如果您绝对希望新分支具有相同的名称,请记住分支基本上只是标签。只需将旧分支重命名为其他名称,然后在提交 #80 处使用您想要的名称创建新分支。
回答by Richard Nader
When doing branch updates from master, I notice that I sometimes over-click, and cause the branch to merge into the master, too. Found a way to undo that.
当从 master 执行分支更新时,我注意到我有时会过度单击,导致分支也合并到 master 中。找到了一种方法来撤销它。
If your last commit was a merge, a little more love is needed:
如果您的最后一次提交是合并,则需要多一点爱:
git revert -m 1 HEAD
git revert -m 1 头
回答by Jianwu Chen
In github, the easy way is to delete the remote branch in the github UI, under branches tab. You have to make sure remove following settings to make the branch deletable:
在 github 中,最简单的方法是在 github UI 中的分支选项卡下删除远程分支。您必须确保删除以下设置以使分支可删除:
- Not a default branch
- No opening poll requests.
- The branch is not protected.
- 不是默认分支
- 没有公开投票请求。
- 分支不受保护。
Now recreate it in your local repository to point to the previous commit point. and add it back to remote repo.
现在在您的本地存储库中重新创建它以指向前一个提交点。并将其添加回远程仓库。
git checkout -b master 734c2b9b # replace with your commit point
Then push the local branch to remote
然后将本地分支推送到远程
git push -u origin master
Add back the default branch and branch protection, etc.
添加回默认分支和分支保护等。