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

Hes*_*sam 223 git git-checkout git-reset

我通过此命令将本地主服务器重置为提交:

git reset --hard e3f1e37
Run Code Online (Sandbox Code Playgroud)

当我输入$ 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
Run Code Online (Sandbox Code Playgroud)

由于我也要重置原点/标题,我结帐到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.
Run Code Online (Sandbox Code Playgroud)

并通过此命令重置标头:

$ git reset --hard e3f1e37
HEAD is now at e3f1e37 development version code incremented for new build.
Run Code Online (Sandbox Code Playgroud)

然后我尝试添加提交到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
Run Code Online (Sandbox Code Playgroud)

最后,我结账给我当地的主人.

$ 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)
Run Code Online (Sandbox Code Playgroud)

因为,我重置了origin/master的头部我希望本地和原点应该在同一个方向,但正如你所看到的那样,git说我的本地/主人是7个提交后面的origin/master.

我该如何解决这个问题?我正在寻找的东西是本地/主人的头和同一提交的起源/主人.下图显示了我的所作所为.谢谢.

在此输入图像描述

Sim*_*ias 520

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
Run Code Online (Sandbox Code Playgroud)

  • 执行请求的操作,但请记住,那些已经从主人那里撤回提交的人会感到不快. (8认同)

小智 47

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

git checkout master
git reset --hard e3f1e37
git push --force origin e3f1e37:master
Run Code Online (Sandbox Code Playgroud)

与接受的答案的主要区别在于push命令中的master之前的提交哈希"e3f1e37:".


小智 16

假设您的分支master在此处和远程都被调用,并且您的远程被调用,origin您可以执行以下操作:

git reset --hard <commit-hash>
git push -f origin master
Run Code Online (Sandbox Code Playgroud)

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