gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 13:52:49 24 4
gpt4 key购买 nike

我推错了分支,我想推新分支但是我推到了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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com