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

问题描述 投票:0回答:4

我们需要及时返回到某个特定的提交。一些意外的改变是为了掌握。尝试恢复它挖得太深,所以 master 的状态很糟糕。我们现在希望master回到66ada4cc61d62afc。

根据 git 恢复到某个提交:

$ 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 git-commit
4个回答
9
投票

快速解决方案:

git push -f

这会强制推送并覆盖远程历史记录。 如果有其他人使用此远程存储库:不要这样做!

从远程存储库拉取的其他人将会遇到问题,因为他们的历史记录不再是存储库历史记录的子集。大多数项目禁止强制推送。

更好的解决方案是为您的更改创建一个新的提交。要么

  • git revert hashes
  • git checkout oldversion .; git add/commit

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


5
投票

git reset
不会恢复您的更改。它只会回到较旧的状态。由于最新的提交已经在远程,并且您没有进行任何本地更改,因此您将无法推送。

使用

git revert <hash>
git revert <hash1>..<hash2>
。这将创建一个新的提交,该提交将恢复您指定的提交的更改。然后推送代码。


0
投票

将主要分支改为不需要的分支。 本地和远程删除main 在好版本结账。 以良好的版本创建新的主分支并作为主分支推送到远程。

不确定这是否正是OP所要求的,但它有效。


-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"
© www.soinside.com 2019 - 2024. All rights reserved.