ChatGPT解决这个技术问题 Extra ChatGPT

Git,如何将 origin/master 重置为提交?

我通过以下命令将本地主机重置为提交:

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

由于我也想重置原点/标头,我结帐到原点/主:

$ 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 次提交。

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

https://i.stack.imgur.com/fuQB3.png

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

S
Simon Boudrias

origin/xxx 分支始终是指向远程的指针。您无法签出它们,因为它们不是指向您的本地存储库的指针(您只签出提交。这就是为什么您不会看到写在命令行界面分支标记中的名称,只有提交哈希)。

更新远程需要做的是强制将本地更改推送到 master:

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 服务器。这意味着您将无法重置原点/主控。您唯一的选择是在新提交中还原更改。
j
jkovacs

找到的解决方案 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 但实际上我在我在 /opt/git 中创建的本地 git 存储库中手动设置了它,以便在这里使用这些想法。我不确定如何或是否可以为 bitbucket、github 等执行此操作...而 @intuitivepixel 这是毫无意义的,因为它逆转了您通过硬重置尝试实现的目标。
嗨@jkovacs,我不希望删除 master 中的新更改。我只想将该提交哈希“e3f1e37”推送到原始主机。是否可以跳过第二个命令 git reset --hard "e3f1e37"?
嗨@jkovacs,我刚刚确认我可以跳过第二步。 :)
M
Mahmoud Zaher

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

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

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


P
Paul-Sebastian

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

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

一个深夜,我偏执地担心我的进度会因硬件故障或其他原因而丢失,我决定将 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

T
Thiago

以下是我的建议。但是,Simon Boudrias 的上述回答也很棒。

查看分支

git checkout master

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

git reset --soft HEAD~1

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

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

然后,进行新的提交,并描述其他情况下发生的情况

git push -f origin master