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

标签 git git-checkout git-reset

我通过这个命令将我的本地 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

因为我也想重置 origin/header,所以我 checkout 到 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.

并通过此命令重置 header :

$ 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

最后,我 checkout 给我的本地主人。

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

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

enter image description here

最佳答案

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

更新远程需要做的是强制将本地更改推送到主服务器:

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

关于Git,如何将 origin/master 重置为提交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17667023/

相关文章:

git-p4 克隆单个文件

git - .gitignore 和删除缓存使得我看不到服务器上的文件?

git - 如何在 SourceTree 本地 checkout fork 的 pull 请求?

git - "git reset"和 "git checkout"有什么区别?

git - 如何恢复未提交的更改,包括文件和文件夹?

git - 'git reset --hard HEAD~1' 和 'git reset --soft HEAD~1' 有什么区别?

git - 无法将一些引用推送到 git@heroku.com

git - 我如何获得 git 手动条目?

Git 从特定目录 check out

git add + git reset 硬删除工作副本文件 - 撤消?