【问题标题】:Git, How to reset origin/master to a commit?Git,如何将 origin/master 重置为提交?
【发布时间】:2013-07-14 01:43:45
【问题描述】:

我通过这个命令将我的本地主机重置为提交:

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/header,我结帐到 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.

然后我尝试将提交添加到我没有成功的原始/标题。

$ 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 的负责人,我希望 local 和 origin 应该在同一个方向,但是正如你所看到的,git 说我的 local/master 比 origin/master 落后 7 次提交。

我该如何解决这个问题?我正在寻找的东西是本地/主控和原产地/主控的负责人指向相同的提交。下图显示了我所做的。谢谢。

【问题讨论】:

  • 首先,确保你被允许强制将代码推送到你的项目中受保护的分支,否则你将无法...

标签: git git-checkout git-reset


【解决方案1】:

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

查看分支

git checkout master

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

git reset --soft HEAD~1

如果不是 master,则从原点手动删除分支

修复你的代码,或者堆叠它。

然后,进行新的提交,并描述所发生的事情 否则

git push -f origin master 

【讨论】:

    【解决方案2】:

    假设您的分支在此处和远程都称为master,并且您的远程称为origin,您可以这样做:

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

    但是,如果其他人正在使用您的远程存储库并提取了您的更改,您应该避免这样做。在这种情况下,最好还原您不想要的提交,然后正常推送。

    【讨论】:

      【解决方案3】:

      由于我有类似的情况,我想我会分享我的情况以及这些答案如何帮助我(谢谢大家)。

      所以我决定在本地工作,每次我想在主分支上保存我的进度时修改我的最后一次提交(我知道,我应该分支出来,提交,继续推送,然后合并回主分支) .

      一个深夜,我偏执地担心我的进度会因硬件故障或其他原因而丢失,我决定将 master 推到 origin。后来我一直在修改我的本地 master 分支,当我决定是时候再次推送时,我遇到了不同的 master 分支,发现我无法像我一样修改 origin/upstream (duh!)本地开发分支。

      所以我没有在本地签出 master,因为我已经在提交之后。师父不变。我什至不需要重置 --hard,我当前的提交没问题。

      我只是强制推送到原点,甚至没有指定我想对 master 强制执行什么提交,因为在这种情况下,它是 HEAD 所在的位置。检查git diff master..origin/master 所以没有任何差异,就是这样。都修好了。谢谢! (我知道,我是git新手,请见谅!)。

      因此,如果您已经可以在本地使用 master 分支,只需:

      git push --force origin master
      git diff master..origin/master
      

      【讨论】:

        【解决方案4】:

        here 找到的解决方案帮助我们将 master 更新为之前已经推送的提交:

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

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

        【讨论】:

        • 不起作用:remote: error: denying non-fast-forward refs/heads/master (you should pull first)
        • @m0skit0 如消息所说的you should pull first :)
        • 这个问题的答案在stackoverflow.com/a/10544328/1019307 - git config receive.denynonfastforwards false 但实际上我在我的本地 git 存储库中手动设置了我在/opt/git 中创建的这里的想法。我不确定如何或是否可以为 bitbucket、github 等执行此操作...而 @intuitivepixel 这是毫无意义的,因为它逆转了您通过硬重置尝试实现的目标。
        • 嗨@jkovacs,我不希望删除master 中的新更改。我只想将该提交哈希“e3f1e37”推送到原始主机。是否可以跳过第二个命令 git reset --hard "e3f1e37"?
        • 嗨@jkovacs,我刚刚确认我可以跳过第二步。 :)
        【解决方案5】:

        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
        

        【讨论】:

        • 执行请求的操作,但请记住,这会让那些已经从 master 中提取提交的人不高兴。
        • 你不应该关心 origin/HEAD,只需将好的 ref 推送到 origin/ref
        • 同意,今天不得不这样做,因为不小心将错误的分支合并在一起,然后推送到原点。它运行良好,但如果其他人一直在从源头检查受影响的分支,它可能会非常具有破坏性。谨慎使用。
        • 不起作用。 remote: error: denying non-fast-forward refs/heads/master (you should pull first)
        • @m0skit0 它确实有效。使用 git-hooks 自定义魔法的是你的 git 服务器。这意味着您将无法重置原点/主控。您唯一的选择是在新提交中还原更改。
        猜你喜欢
        • 1970-01-01
        • 2020-07-27
        • 1970-01-01
        • 2021-02-07
        • 2022-01-10
        • 2012-05-22
        • 2012-01-31
        • 2011-02-22
        相关资源
        最近更新 更多