在 Github 上反转最后一次推动。

Reverse last push on Github.

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

git log 

这将为您提供所有提交的日志,然后 git reset --hard "commit id" 这将重置为给定的头

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

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

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

结帐到新分支(例如,feature)。添加、提交、推送到远程分支(此处 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并执行强制推送更新远程主控(删除远程主控的最后一次提交)

$ git checkout master
$ git push -f origin HEAD

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


备选: 如果您没有强制推送权限或其他人已拉取 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

创建一个新分支并cherry-pick最后一次提交并推送到远程。

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