1

I wanted to revert the last push on branch branch1 and I used this command when I was on branch1:

git push -f origin HEAD^:master

and instead of writing git push -f origin HEAD^:branch1. The result is that my master become branch1. Now I want to bring back my old master. Can I do that?

Narek
  • 38,779
  • 79
  • 233
  • 389

1 Answers1

2

This answer assumes your local master branch was upto date with origin/master.

Yes, you can force push local master on the remote master again:

git push -f origin master:master

You don't need to be explicit on to what you are pushing here, because git will assume same name branches, so you can shorten it to:

git push -f origin master
Sébastien Dawans
  • 4,537
  • 1
  • 20
  • 29
  • 1
    This is wrong if the local master does not equal origin/master~1 – chelmertz Aug 28 '15 at 10:52
  • my understanding is that he pushed accidentally to master rather than ``branch1``, and wants to restore master – Sébastien Dawans Aug 28 '15 at 10:53
  • 1
    @chelmertz and yes, my answer assumes that his local master was upto date before overwriting the remote master (I see what you mean now) – Sébastien Dawans Aug 28 '15 at 10:54
  • +1 to your last comment, if you add that to your answer, you get +1 there too :) Otherwise: can you provide an answer that takes care of the other scenario, i.e. when his master is not in sync with origin/master but still wants to revert his wrong push? – chelmertz Aug 28 '15 at 10:56
  • I have used `git push -f origin SHA1_to_old_master:master` – Narek Aug 28 '15 at 11:07