git 回滚到上一次提交 - 用于 MAC 的 Github(恢复已在进行中)

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

Rollback to Previous Commit - Github for MAC (a revert is already in progress)

gitversion-controlgithubgithub-for-mac

提问by tGilani

I think I've messed up here.

我想我在这里搞砸了。

I made a few changes to my code from a last commit adding new functionalities and realized that some other piece of code was now acting strangely. I decided to Roll back to an old commit (pushed to remote as well) in order to test if that functionality was working by then.

我从上次提交中对我的代码进行了一些更改,添加了新功能,并意识到其他一些代码现在表现得很奇怪。我决定回滚到一个旧的提交(也推送到远程),以测试该功能在那时是否有效。

Before I pressed Rollback, I committed my currently made changes because I did not want to lose them. After committing (not pushing to remote), I did the rollback to that old commit. (note that there were quite a few commits inbetween the commit i roll backed to and the one i committed just now).

在按下回滚之前,我提交了当前所做的更改,因为我不想丢失它们。提交后(不推送到远程),我回滚到那个旧提交。(请注意,在我回滚的提交和我刚刚提交的提交之间有很多提交)。

All worked fine and my code reverted to that commit. The functionality was misbehaving there in that commit as well so I decided to come back to my most recent commit.

一切正常,我的代码恢复到该提交。该功能在该提交中也存在不当行为,因此我决定回到我最近的提交。

However, I knew not how to do that except Rolling Back to the latest commit. But it gave me an error.

但是,除了回滚到最新提交之外,我不知道该怎么做。但它给了我一个错误。

error: a cherry-pick or revert is already in progress
hint: try "git cherry-pick (--continue | --quit | --abort)"
fatal: revert failed
(32768)

Now, it seems like most of things are back but the current version of code isn't the same as my last commit. It is somewhere in between. =(

现在,似乎大部分事情都回来了,但当前版本的代码与我上次提交的不同。它介于两者之间。=(

What did I do wrong? [I'm not asking out of innocence, I know I did it wrong ;)]

我做错了什么?[我不是出于无辜而问,我知道我做错了;)]

What was the right way of doing it? [I think I should have branched first]

正确的做法是什么?[我想我应该先分支]

回答by Eugene Sajine

I do not know what github for Mac rollback does, but it seems that you would be better off using command line to resolve the issue at hand:

我不知道 github for Mac 回滚是做什么的,但似乎最好使用命令行来解决手头的问题:

git cherry-pick --abort- to stop any cherry-picking in progress

git cherry-pick --abort- 停止任何正在进行的樱桃采摘

git branch -va- will show you where are your pointers right now

git branch -va- 会告诉你你的指针现在在哪里

make sure your working directory is clean: git status- should not show any modified or staged files

确保您的工作目录是干净的: git status- 不应显示任何修改或暂存的文件

git stash- if anything modified still present

git stash- 如果任何修改仍然存在

git reset --hard your_local_branch github/remote_branch- make local branch reflect the state as it is on the remote side. obviously you don't need to do a reset if your current branch will point to the same commit as the remote. If you're in detached HEAD state (git status will tell you about it) then to come back to the normal state just checkout your local branch.

git reset --hard your_local_branch github/remote_branch- 使本地分支反映远程端的状态。显然,如果您当前的分支将指向与远程相同的提交,则您不需要进行重置。如果您处于分离的 HEAD 状态(git status 会告诉您有关它的信息),那么要回到正常状态,只需检查您的本地分支即可。

Now decide what you actually want to achieve:

现在决定你真正想要实现的目标:

I. get rid of the faulty commit?

I. 摆脱错误的提交?

Use interactive rebase and remove the line with faulty commit, then force push to the remote repo on github (say faulty commit happened 10 commits ago)

使用交互式 rebase 并删除提交错误的行,然后强制推送到 github 上的远程仓库(假设错误提交发生在 10 次提交之前)

git rebase -i HEAD~11

git rebase -i HEAD~11

II. revert faulty commit? - wouldn't recommend to do that after some other commits, unless you're absolutely sure that the following commits didn't touch the same piece of code. This will effectively create a reverse commit (if line was added by faulty commit it will be removed by revert and vice versa)

二、恢复错误的提交?- 不建议在其他一些提交之后这样做,除非您绝对确定以下提交没有触及同一段代码。这将有效地创建一个反向提交(如果行是由错误提交添加的,它将被还原删除,反之亦然)

git revert {commit-sha1}

git revert {commit-sha1}

III. Amend faulty commit? use interactive rebase, but instruct it to stop at faulty commit for amending. When it does stop edit the change and continue rebasing, then force push to the remote branch (use rebase command from the solution I )

三、修改错误提交?使用交互式变基,但指示它在错误提交处停止以进行修改。当它停止编辑更改并继续变基时,然后强制推送到远程分支(使用解决方案 I 中的变基命令)

After you're done if anything was stashed use git stash popto bring the changes back.

完成后,如果隐藏了任何内容,请使用git stash pop以恢复更改。

hope that helps!

希望有帮助!