Habib
Habib

Reputation: 928

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?

Upvotes: 7

Views: 16889

Answers (2)

Sajib Khan
Sajib Khan

Reputation: 24136

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     

Upvotes: 12

Moses Nandwa
Moses Nandwa

Reputation: 179

git log 

This will give you a log of all the commits and then git reset --hard "commit id" This will reset to the given head

Upvotes: 0

Related Questions