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


当前回答

这是我的方法:

假设分支名为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

其他回答

这将删除您推送的提交

git reset --hard 'xxxxx'

git clean -f -d

git push -f

做个备份以防万一

评论中出现了两个注释:

'xxxxx'是你想保留的最后一次提交的哈希码。 清洁是危险的。它删除文件,不受版本控制!

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

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

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

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

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

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

这是我的方法:

假设分支名为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

假设61234是您希望保持的最后一个良好提交的sha-number。

git reset --hard 61234
git push -f

将完全删除所有错误的提交,没有任何痕迹。

注意:如果你想推送(你重置的提交)到一个特定的分支,你可以使用git push -f origin branch-name来代替。