Git - Accidentally pushed to upstream instead of my branch

Revert locally and push your revert

git revert <commit_id>
git push upstream/master

This is better than deleting your history if you're collaborating with a team on the upstream repo. If you do hard reset and force push, you could end up removing other commits by other people. It's better to just roll back your changes and show that in the team history.


git checkout -b myfeaturebranch
git checkout master
git reset --hard HEAD~1
git push --force

This does the following:

  1. Creates a new local branch with the commits you made
  2. Goes back to the master branch
  3. Resets the commit pointer back one commit
  4. Pushes the reset which removes the commit from the remote

The danger here is if anyone else pulled from master those commits will be lost. In that case, you'll want to use revert instead of reset which will record the reset as part of the commit history.

git checkout -b myfeaturebranch
git checkout master
git revert HEAD~1
git push

Tags:

Git

Github