我们需要及时回到特定的提交。对Master进行了一些意外更改。尝试将其恢复挖得太深,因此master处于不良状态。现在,我们希望主服务器回到66ada4cc61d62afc。
根据git还原回某些提交:
$ git reset --hard 66ada4cc61d62afc
HEAD is now at 66ada4c Updated documentation
Run Code Online (Sandbox Code Playgroud)
然后,尝试提交:
$ git add *.h *.cpp
$ git commit -m "Go back to Commit 66ada4cc61d62afc"
On branch master
Your branch is behind 'origin/master' by 16 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)
最后:
$ git push
To https://github.com/weidai11/cryptopp.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/weidai11/cryptopp.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.
Run Code Online (Sandbox Code Playgroud)
现在,一切都正是我想要的。我不知道为什么Git遇到麻烦,以及Git在说什么。如果Git做到了,那肯定会很好。但是可惜的是,Git使每项简单的任务变得困难,并且将造成不必要的痛苦。
如何提交并推送更改?
git reset不会还原您的更改。它只会回到旧状态。由于最新的提交已经在远程,并且您没有进行任何本地更改,因此您将无法推送。
使用git revert <hash>或git revert <hash1>..<hash2>。这将创建一个新提交,该提交还原您指定的提交的更改。然后推送代码。
快速解决方案: git push -f
这将强制推送并覆盖远程历史记录。如果还有其他人在使用此远程存储库,请不要这样做!
从远程存储库中拉出的其他人会遇到问题,因为他们的历史记录不再是存储库历史记录的子集。大多数项目禁止强制推送。
更好的解决方案是为更改创建一个新的提交。要么
git revert hashes 要么git checkout oldversion .; git add/commit然后,这将在您的历史记录之上创建一个新的提交,该提交描述了返回到所需版本所需的更改。
jww*_*jww -3
为了避免部分解决方案只是将一组错误交换为另一组错误,我执行了以下操作:
git clone https://github.com/weidai11/cryptopp.git cryptopp-old
git clone https://github.com/weidai11/cryptopp.git cryptopp-new
cd cryptopp-old
git checkout 66ada4cc61d62afc
cd ../cryptopp-new
cp ../cryptopp-old/*.h ../cryptopp-old/*.cpp .
git commit -am "Go back to Commit 66ada4cc61d62afc"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5115 次 |
| 最近记录: |