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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17667023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 16:33:58  来源:igfitidea点击:

Git, How to reset origin/master to a commit?

gitgit-checkoutgit-reset

提问by Hesam

I reset my local master to a commit by this command:

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

git reset --hard e3f1e37

when I enter $ git statuscommand, terminal says:

当我输入$ 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

Since I want to reset origin/header as well, I checkout to 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.

and reset the header by this command:

并通过以下命令重置标头:

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

Then I tried to add commit to origin/header that I was not successful.

然后我尝试向 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

Finally, I checkout to my local master.

最后,我结帐给我当地的主人。

$ 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)

Since, I reset the head of origin/master I expect local and origin should be in same direction but as you see, git is saying that my local/master is behind origin/master by 7 commits.

因为,我重置了 origin/master 的负责人,我希望 local 和 origin 应该在同一方向,但正如你所看到的,git 说我的 local/master 落后于 origin/master 7 次提交。

How can I fix this issue? The things that I'm looking for is Head of local/master and origin/master point to same commit. Following image shows what I did. Thanks.

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

enter image description here

在此处输入图片说明

回答by Simon Boudrias

origin/xxxbranches are always pointer to a remote. You cannot check them out as they're not pointer to your local repository (you only checkout the commit. That's why you won't see the name written in the command line interface branch marker, only the commit hash).

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

What you need to do to update the remote is to force push your local changes to 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

回答by jkovacs

The solution found herehelped us to update master to a previous commit that had already been pushed:

此处找到的解决方案帮助我们将 master 更新到先前已推送的提交:

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

The key difference from the accepted answer is the commit hash "e3f1e37:" before master in the push command.

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

回答by Mahmoud Zaher

Assuming that your branch is called masterboth here and remotely, and that your remote is called originyou could do:

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

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

However, you should avoid doing this if anyone else is working with your remote repository and has pulled your changes. In that case, it would be better to revert the commits that you don't want, then push as normal.

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

回答by Paul-Sebastian Manole

Since I had a similar situation, I thought I'd share my situation and how these answers helped me (thanks everyone).

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

So I decided to work locally by amending my last commit every time I wanted to save my progress on the main branch (I know, I should've branched out, committed on that, kept pushing and later merge back to master).

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

One late night, in paranoid fear of loosing my progress to hardware failure or something out of the ether, I decided to push master to origin. Later I kept amending my local master branch and when I decided it's time to push again, I was faced with different master branches and found out I can't amend origin/upstream (duh!) like I can local development branches.

一个深夜,出于对硬件故障或其他原因而失去进度的偏执恐惧,我决定将 master 推到原点。后来我一直在修改我的本地 master 分支,当我决定是时候再次推送时,我面临着不同的 master 分支,发现我不能像本地开发分支一样修改 origin/upstream(duh!)。

So I didn't checkout master locally because I already was after a commit. Master was unchanged. I didn't even need to reset --hard, my current commit was OK.

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

I just forced push to origin, without even specifying what commit I wanted to force on master since in this case it's whatever HEAD is at. Checked git diff master..origin/masterso there weren't any differences and that's it. All fixed. Thanks! (I know, I'm a git newbie, please forgive!).

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

So if you're already OK with your master branch locally, just:

因此,如果您已经对本地的 master 分支感到满意,只需:

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