git 远程回退到先前的提交

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

Rewind remote to a prior commit

git

提问by Jesse Hallam

As a junior git user, I got overwhelmed by a tough merge and must have done something wrong. I ended up committing my conflict resolutions with a whole mess of garbage inside my source files. The commit shows additions of many lines which look like <<<<<<< HEADand >>>>>>> a7b4de79431c2e73d28621c72c8d14820df1a24b. The commit has been pushed to remote origin already so I unfortunately can't just ammend the commit.

作为一名初级 git 用户,我被一个艰难的合并弄得不知所措,一定是做错了什么。我最终在我的源文件中提交了一堆乱七八糟的垃圾来解决冲突。提交显示了许多看起来像<<<<<<< HEAD和 的行的添加>>>>>>> a7b4de79431c2e73d28621c72c8d14820df1a24b。提交已经被推送到远程源,所以不幸的是我不能修改提交。

I want to rewind the remote repository to the last good commit, 4a3ba7b0e56cf0be80274c1f879029220a889bdeand (if possible) destroy the bad commit d004651972cbc35f70ee5a2145b6e03169c77279.

我想将远程存储库倒回到最后一个好的提交,4a3ba7b0e56cf0be80274c1f879029220a889bde并且(如果可能)销毁坏的提交d004651972cbc35f70ee5a2145b6e03169c77279

I tried:

我试过:

git checkout 4a3ba7
git push -f

and got: fatal: You are not currently on a branch.

并得到: fatal: You are not currently on a branch.

回答by loganfsmyth

checkoutmoves your current working directory to a previous commit, but it does not modify branch contents. You need to reset the branch back to an old commit, and then push that.

checkout将您当前的工作目录移动到以前的提交,但它不会修改分支内容。您需要将分支重置回旧的提交,然后推送它。

git checkout ...
git reset --hard 4a3ba7
git push -f

that said, if you are already push -fing to change only the most recent commit, you should be able to use --amend.

也就是说,如果您已经push -f只更改最近的提交,您应该能够使用--amend.

git checkout ...
// Fix the file
git commit --amend
git push -f

If there are at least some changes that you want that were committed after 4a3ba7then you can also do this:

如果至少有一些您想要在4a3ba7之后提交的更改,您也可以这样做:

git checkout ...
git reset 4a3ba7
git add -p
// Use the interactive prompt to choose the parts you want
git commit
git push -f

Update

更新

Your error remote: error: denying non-fast-forward refs/heads/masteris because the git server that you are using, Assembla, does not allow rewriting history by default. See this answer for fixing that part: Undo git push to Assembla

您的错误remote: error: denying non-fast-forward refs/heads/master是因为您使用的 git 服务器 Assembla 默认不允许重写历史记录。请参阅此答案以修复该部分:Undo git push to Assembla

回答by Marco Leogrande

You don't need to checkout things locally to rewind a remote branch; you can just use

你不需要在本地签出东西来回滚远程分支;你可以使用

git push -f origin 4a3ba7b0:master

Of course double check your logs before doing anything, as this push willoverwrite remote data.

当然,在做任何事情之前都要仔细检查你的日志,因为这个推送覆盖远程数据。

If you receive permission errors, receive.denyNonFastForwardsmight be set to truein the remote repository; you have to change that for a rewind to work in any case.

如果收到权限错误,receive.denyNonFastForwards可能true在远程仓库中设置为;在任何情况下,您都必须更改它才能倒带。

回答by bssstudio

You can do a

你可以做一个

git reset --hard *commithash* 

but be warned: This can result in loss of modified data! (You have been warned :))

但请注意:这可能会导致修改后的数据丢失!(你被警告了 :))