Github: 重置为之前的提交

Github: reset to previous commit

小团队。一位同事误推到origin:master。他已经重置了他的本地仓库,但不能 push -f 到 Github 因为仓库是 protected.

我已经 fetch 编辑了 repo,但还没有将他的错误提交合并到我的本地 master...但是。

假设我可以 push -f 到原点,我怎么能在 Github 上重置 origin 以使其反映他犯错之前的状态?

$ git push origin 2860a4c:master
To github.com:example/myproj.git
 ! [rejected]        2860a4c -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:example/myproj.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.

我真的需要先整合错误提交(与 git pull),然后我假设 reset hard 2860a4c 然后 push -f origin

我只是不想让事情变得更糟。

如果可以push -f,那就去做吧。如果你想要的状态是top之前的一个commit,你可以运行

git pull
git reset --hard @~1
git push -f

确保团队中的其他每个人都保持同步,这样他们就不会丢失任何工作。

你不需要拉坏提交,因此 git reset 也不需要。 如果我正确理解了这个问题 git push --force 应该就足够了。它会将遥控器的状态带到您的状态。

如果您已经提取了错误的提交,请使用 git reset --hard + git push --force,如下所示:.

以下是您可以执行的步骤,假设 您拥有 git push -f 的权限。

在您的机器上,执行:

# Step 1: Take the changes from remote
git pull

# Step 2: Note the commit to which you want for restoring your repo to 
# using `git log`. Say the commit id is "x". 
git log

# Step 3: Do hard reset for that commit. 
#         ** NOTE ** All the changes after the commit "x" will be removed
git reset --hard x    # where x is the commit id

# Step 4: Push to remote
git push -f

然后在同事的机器上,执行step 1step 3然后执行git pull合并远程更改


如果 您没有 git push -f 的权限,请执行:

git pull

git revert <commit id>   # may get it via "git log"

git push

使用 git revert,还原提交的更改将被删除,但此提交将保留在提交历史记录中。