Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - Accidentally pushed to upstream instead of my branch

Tags:

git

github

I am new to git. I wanted to push my data to my branch but instead I pushed it upstream/master branch. I want revert that push. Is there any way?

Any help is appreciated.

like image 538
user1079065 Avatar asked May 05 '16 21:05

user1079065


2 Answers

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.

like image 98
yelsayed Avatar answered Nov 19 '22 22:11

yelsayed


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
like image 18
Jordan Parmer Avatar answered Nov 19 '22 22:11

Jordan Parmer