I have pushed on the wrong branch, I want to push on new branch but I have pushed on the master
branch. Is there any way to Reverse the last committed push and get the last code and pushed again to the new branch?
Undo the last commit by soft reset from local master
branch and keep the changes locally (in working tree).
$ git checkout master
$ git reset --soft HEAD~1
$ git log # make sure the last commit is reverted successfully as you expect.
Checkout to a new branch (say, feature
). Add, Commit, Push to remote branch (feature
here).
$ 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
Back to local master
and do force push to update the remote master (delete remote master's last commit)
$ git checkout master
$ git push -f origin HEAD
N.B: Force push needed since changing the history of the remote master.
Alternate: If you don't have force push permission or someone else Pulled the origin/master
and got your last commit already. Then better you revert the last commit instead reset (history change).
$ 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
Create a new branch and cherry-pick the last commit and push to remote.
$ git checkout -b feature
$ git cherry-pick <last-commit-hash>
$ git push origin HEAD
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With