Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse last push on Github.

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?

like image 442
Habib Avatar asked Jun 07 '17 05:06

Habib


1 Answers

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     
like image 76
Sajib Khan Avatar answered Nov 03 '22 08:11

Sajib Khan