How to undo a git push

I issued a github pull request for a non-profit / charity project I was working on. I have already forked his git repo, and I pushed my changes to my fork in github. After looking at how other contributors issue their pull requests (request to merge a branch to jorgej’s master branch), I realised that I issued a pull-request from my master branch. It was actually a bit awkward to develop and make changes on my master branch, it makes it difficult to merge updates from jorgej’s upstream remote master branch.

So what I wanted to do was:

  1. close my initial pull request
  2. create a branch starting from jorgej’s latest commit (e.g. fix_branch)
  3. cherry-pick my commit to that branch
  4. issue a pull request to merge from my ‘fix_branch’ to jorgej’s master branch
  5. reset my master branch (on my github fork) to jorgej’s latest commit
  6. also needed to reset my master branch in my local machine, i.e.: git reset --hard HEAD^
step 1 – 4 was pretty trivial, but I’m not sure how to reset my master branch to a previous state. (Since I have already pushed this commit)
Googling — found this really helpful post. Essentially I can force a git push to point to a certain head. Awesome! I thought this was pretty useful, so I’m just going to paste Charles Bailey’s response here:
——————————————————————————–

You need to make sure that no other users of this repository are fetching the incorrect changes or trying to build on top of the commits that you want removed because you are about to rewind history.Then you need to ‘force’ push the old reference.

git push -f origin cc4b63bebb6:alpha-0.3.0 

You may have receive.denyNonFastForwards set on the remote repository. If this is the case, then you will get an error which includes the phrase [remote rejected].

In this scenario, you will have to delete and recreate the branch.

 
git push origin :alpha-0.3.0 
git push origin cc4b63bebb6:refs/heads/alpha-0.3.0

If this doesn’t work – perhaps because you have receive.denyDeletes set, then you have to have direct access to the repository. In the remote repository, you then have to do something like the following plumbing command.

git update-ref refs/heads/alpha-0.3.0 cc4b63bebb6 83c9191dea8
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: