Git,如何将origin/master重置到某个提交?

338
我将本地主分支重置到一个提交点,使用以下命令:
git reset --hard e3f1e37

当我输入$ git status 命令时,终端会显示以下信息:
# On branch master
# Your branch is behind 'origin/master' by 7 commits, and can be fast-forwarded.

#   (use "git pull" to update your local branch)
#
nothing to commit, working directory clean

由于我也想重置起点/标头,因此我检出到 origin/master:

$ git checkout origin/master
Note: checking out 'origin/master'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 2aef1de... master problem fixed for master. its okay now.

通过此命令重置标题:

$ git reset --hard e3f1e37
HEAD is now at e3f1e37 development version code incremented for new build.

然后我试图向origin/header添加提交,但没有成功。

$ git commit -m "Reverting to the state of the project at e3f1e37"
# HEAD detached from origin/master
nothing to commit, working directory clean

最后,我将代码检出到我的本地主分支。

$ git checkout master
Switched to branch 'master'
Your branch is behind 'origin/master' by 7 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

由于我重置了origin/master的head,所以我期望本地和origin应该处于相同的方向,但是如你所见,git说我的local/master比origin/master落后7次提交。

我该如何解决这个问题?我想要的是local/master和origin/master指向相同的提交。以下图片展示了我做了什么。谢谢。

enter image description here


首先,请确保您被允许向项目上的受保护分支强制推送代码,否则您将无法进行推送。 - DarmVillegas
6个回答

729

origin/xxx分支始终指向远程,您不能将其作为指向本地存储库的指针进行检出(您只检出提交。这就是为什么您不会在命令行界面的分支标记中看到名称,只有提交哈希)。

要更新远程内容,您需要强制将本地更改推送到主分支:

git checkout master
git reset --hard e3f1e37
git push --force origin master
# Then to prove it (it won't print any diff)
git diff master..origin/master

14
请执行所需的操作,但请记住,这将使那些已经从主分支拉取提交的人感到不满。 - mnagel
1
你不需要关心origin/HEAD,只需将好的引用推送到origin/ref即可。 - Simon Boudrias
1
同意,今天不小心将错误的分支合并后推送到源代码库,只好这样做。它运行良好,但如果其他人已经从源代码库检出受影响的分支,则可能会非常破坏性。请谨慎使用。 - Nick W.
1
无法工作。remote: error: denying non-fast-forward refs/heads/master (you should pull first) - m0skit0
1
@m0skit0,它确实有效。是你的git服务器在使用git-hooks进行自定义操作。这意味着你将无法重置origin/master。你唯一的选择是在一个新的提交中还原更改。 - Simon Boudrias
显示剩余14条评论

76

在这里找到的解决方案帮助我们将主分支更新到一个已经被推送的先前提交:

git checkout master
git reset --hard e3f1e37
git push --force origin e3f1e37:master

与被接受答案的主要区别在于推送命令中"e3f1e37:"提交哈希值在master之前。


1
无法工作:remote: error: denying non-fast-forward refs/heads/master (you should pull first) - m0skit0
正如消息所说,“@m0skit0,你应该先拉取”。 :) - intuitivepixel
答案在此处:https://dev59.com/Zmkv5IYBdhLWcg3wghEh#10544328 - git config receive.denynonfastforwards false 但实际上我在本地的 git 存储库 /opt/git 中手动设置了这个。那是我用来尝试这里的想法。我不确定如何或者是否可以在 bitbucket、github 等平台上做到这一点。而且@intuitivepixel 这样做是无意义的,因为它会撤销你尝试使用硬重置所达到的效果。 - HankCa
嗨@jkovacs,我不想删除主分支中的新更改。我只想将提交哈希“e3f1e37”推送到原始主分支。跳过第二个命令git reset --hard "e3f1e37"是可能的吗? - KarenAnne
嗨@jkovacs,我刚刚确认我可以跳过第二步。 :) - KarenAnne
如果你的分支出现了严重问题,你想要将其恢复到可工作状态,并且没有其他人在远程上进行操作,那么这就是解决方法。 - Keith Pickering

16
假设您的本地分支和远程分支都被称为master,且您的远程库被称为origin,那么您可以执行以下操作:

假设您的本地分支和远程分支都被称为master,且您的远程库被称为origin,那么您可以执行以下操作:

git reset --hard <commit-hash>
git push -f origin master

然而,如果有其他人正在使用你的远程仓库并且已经拉取了你的更改,你应该避免这样做。在那种情况下,最好撤销你不想要的提交,然后像平常一样推送。


1
由于我遇到了类似的情况,所以我想分享一下我的经历以及这些答案是如何帮助我的(感谢大家)。
因此,我决定每次想要在主分支上保存进度时,都通过修改最后一次提交来在本地工作(我知道,我应该分支出去,在那里提交,保持推送并稍后合并回主分支)。
深夜,我担心硬件故障或其他无法预见的事情会导致我的进展丢失,于是决定将主分支推送到远程。后来,我不断修改本地主分支,当我决定再次推送时,我面临着不同的主分支,并发现我无法像本地开发分支一样修改origin/upstream(当然!)。
因此,我没有在本地检出主分支,因为我已经完成了一次提交。主分支没有改变。我甚至不需要重置--hard,我的当前提交是可以的。
我只是强制推送到远程,甚至没有指定我想要强制推送到主分支的提交,因为在这种情况下,它取决于HEAD是什么。检查git diff master..origin/master,所以没有任何差异,就这样。全部解决。谢谢!(我知道,我是Git新手,请原谅!)。
因此,如果您已经满意于本地的主分支,请执行以下操作:
git push --force origin master
git diff master..origin/master

1

这是我的建议。不过,Simon Boudrias 上面的回答也很棒。

检查分支

git checkout master

软重置,意味着这些更改将变为未提交状态

git reset --soft HEAD~1

如果不是主分支,请手动从源中删除该分支

修复您的代码,或者堆栈它。

然后,使用描述发生了什么的新提交,否则。

git push -f origin master 

-2

步骤1. 重置分支到特定的HEAD。 步骤2. 强制推送更改到你的远程分支。

 git reset --hard e3f1e37 /   git reset --hard origin/master
 git push --force origin "Branch name"

完成。现在使用重置到之前的提交来检查您的远程分支


此解决方案已经由接受的答案提供。 - snakecharmerb

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接