我在远程存储库中有一个项目,与本地存储库(开发)和服务器存储库(prod)同步。我一直在做一些已经推送到远程和从服务器上拉出来的承诺更改。现在,我想撤销这些更改。因此,我可以在更改之前签出到提交,并提交新更改,但我猜再次将它们推到远程会有问题。我该怎么做,有什么建议吗?


当前回答

简单方法:

git reset <commit_hash>

(您希望保留的最后一个提交的哈希值)。

您将在本地保留当前未提交的更改。

如果你想再推一次,你必须做到:

git push -f

其他回答

你可以这样做

git push origin +<short_commit_sha>^:<branch_name>
git revert HEAD -m 1

在上面的代码行。"最后一个参数表示"

1 -还原一次提交。 2 -返回最近两次提交。 N -返回上一个N 提交。

该命令后需要按一下才能对远程生效。您还有其他选项,例如指定要恢复的提交范围。这是选项之一。


稍后使用git commit -am "COMMIT_MESSAGE" 然后是git push或git push -f

这是我的方法:

假设分支名为develop。

# Checkout a new temp branch based on one history commit
# (command to get history commit hashes: git log --oneline)
git checkout <last_known_good_commit_hash>

# Delete the original develop branch 
git branch -D develop
# Create a new develop branch based on the temp branch
git checkout -b develop

# Force update the remote with the new branch
git push -f origin develop

一种不保留“撤销”痕迹的解决方案。

注意:如果有人已经抽取了您的更改,则不要这样做 (我只会在个人回购时使用这个方法。)

run:

git reset <previous label or sha1>

这将在本地重新签出所有更新(因此git状态将列出所有更新的文件)

然后你“做你的工作”并重新提交你的更改(注意:这一步是可选的)

git commit -am "blabla"

此时,您的本地树与远程树不同

git push -f <remote-name> <branch-name>

将强制远程分支接受此推送并删除前一个推送(指定remote-name和branch-name不是强制的,但建议避免更新带有update标志的所有分支)。

!! 注意一些标签可能仍然指向删除提交!!how-to-delete-a-remote-tag

Git重置<commit_hash>(获取<commit_hash>使用Git log——oneline)

Git恢复。将所有更改的文件恢复到yout目标提交的版本

Git push origin master——force强制推到远程主分支。但如果有人和你在同一分支工作,在使用力推时要小心