返回存储库中的特定修订后提交并推送更改?

Commit and push changes after going back to a particular revision in the repository?

我们需要及时回到特定的提交。对 master 进行了一些意外更改。试图恢复它挖得太深,所以主人处于糟糕的状态。我们现在希望 master 回到 66ada4cc61d62afc。

根据git revert back to certain commit

$ git reset --hard 66ada4cc61d62afc
HEAD is now at 66ada4c Updated documentation

然后,尝试提交它:

$ 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

最后:

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

现在,一切都如我所愿。我不知道为什么 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

这会在您的历史记录之上创建一个新的提交,描述返回到您想要的任何版本所需的更改。

为了避免将一组错误简单地换成另一组错误的部分解决方案,我执行了以下操作:

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"