我将自己的存储库从github
克隆到本地计算机,进行一些小改动,然后git push
。 git log
喜欢这个:
3: xxxxx
2: yyyyy
1: zzzzz
我突然发现commit 3
中存在错误,因此在本地存储库中,我首先执行git reset 2
,然后再次修改该文件,然后git commit
。因此git log
更改为:
4: ttttt
2: yyyyy
1: zzzzz
这次git push origin
,git
抱怨:
To https://github.com/NanXiao/torch.git
! [rejected] patch-1 -> patch-1 (non-fast-forward)
error: failed to push some refs to 'https://github.com/NanXiao/torch.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
那么如何恢复远程存储库呢?像这样制作远程存储库git log
:
4: ttttt
2: yyyyy
1: zzzzz
答案 0 :(得分:2)
为了回顾一下你实际对你的分支做了什么,你开始用这个:
3: xxxxx
2: yyyyy
1: zzzzz
您认为提交3
存在问题,因此您使用git reset --hard HEAD~1
它,并留下您的信息:
2: yyyyy
1: zzzzz
然后,你在这个分支上做了一个新的提交:
4: ttttt
2: yyyyy
1: zzzzz
您看到的non-fast-forward
错误是有道理的,因为Git无法简单地在远程分支上添加提交4
,这仍然以提交3
或<结尾/ em>以另一个提交结束,其他人在提交3
之上做了。
话虽如此,如果您想强制当前分支到存储库,您可以通过以下方式执行此操作:
git push origin your_branch --force
在进行强制推动时请注意,因为它可能会导致正在与您的分支机构合作的其他任何人出现问题。