git - 反转 Github 上的最后一次推送。

标签 git github push branch

我推错了分支,我想推新分支但是我推到了master分支。有什么办法可以反转最后提交的推送并获取最后的代码并再次推送到新分支?

最佳答案

通过软重置从本地 master 分支撤消最后一次提交,并将更改保留在本地(在工作树中)。

$ git checkout master
$ git reset --soft HEAD~1

$ git log               # make sure the last commit is reverted successfully as you expect.

checkout 到新分支(例如,feature)。添加、提交、推送到远程分支(此处为功能)。

$ git checkout -b feature   # checkout new branch with the local changes
$ git status                # see the changed files
$ git add .
$ git commit -m 'message'
$ git push origin HEAD

回到本地master并执行force push更新远程master(删除远程master的最后一次提交)

$ git checkout master
$ git push -f origin HEAD

N.B:自从更改远程主机的历史以来需要强制推送。


备用:如果您没有强制推送权限或其他人 pull 了 origin/master 并且已经获得了您的最后一次提交。那么最好还原上次提交而不是重置(历史更改)。

$ git checkout master
$ git log                        # copy the last-commi-hash
$ git revert <last-commit-hash>
$ git push origin HEAD           # note, no force push is needed

创建一个新分支并挑选最后一次提交并推送到远程。

$ git checkout -b feature
$ git cherry-pick <last-commit-hash>
$ git push origin HEAD     

关于git - 反转 Github 上的最后一次推送。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44404321/

相关文章:

git - 如何使用android studio更改git存储库

c# - 使用 Git 和 Visual Studio 检查项目

Git 说 origin 已经存在,但实际上并不存在

git - git merge 后,这个分支落后1次提交

ios - 使用 Firebase iOS Swift 将通知从特定设备推送到特定设备

c - 使用C、winsock(windows)在三星Tizen推送服务器中推送消息

Git 提交来自 pull 请求的消息

git - 如何恢复 git bisect

php - 将 Git 与 Wordpress 结合使用

javascript - JS/JQUERY TAB 具有历史记录、兼容性