8

We had a snafu tonight where code checked into git (which gets auto pushed to our application if pushed to the master branch) that caused our site to go down for a bit. I restored an EC2 snapshot to get the site back up, but now I need to cleanup git and get us back on track.

It looks like I need to find the last successful commit to the master branch, grab the first 8 or so characters of it's sha1 id, and run this:

git reset --hard jfe2ldj2
git push origin master -f

Once I do that, everything on the master branch from commit "jfe2ldj2" an later will be wiped from git and can never be recovered. Am I understanding that correctly?

Also, this will not effect any other branches or commits correct? Meaning, that once I run that command and bring the master branch back to say 6 weeks ago, all of the other branches will remain current. Meaning that if I have a number of release and feature branches and they all have multiple commits since 6 weeks ago, all of those branches and commits will still be there?

Kris Anderson
  • 1,007
  • 2
  • 9
  • 15
  • Consider reading ["Reset demystified"](http://git-scm.com/2011/07/11/reset.html) by Scott Chacon. – kostix Apr 08 '13 at 09:16
  • @kostix link is broken – Aiden Cullo Feb 01 '23 at 17:54
  • @AidenCullo, that article has since then been reworked to be a part of "The Book" on Git, so [here it is](https://git-scm.com/book/en/v2/Git-Tools-Reset-Demystified). Anyway, I like the original a bit more—if only for a nice pic of the three trees ;-)—so [here is its archive copy](http://web.archive.org/web/20120511055637/http://git-scm.com/2011/07/11/reset.html). – kostix Feb 02 '23 at 10:01

1 Answers1

10

Once I do that, everything on the master branch from commit jfe2ldj2 an later will be wiped from git and can never be recovered.

As has already been well covered, if changes were committed they can still be recovered even after reset --hard

Also, this will not effect any other branches or commits correct?

Yes, think of a tree, chopping off one branch leaves the others intact.

Community
  • 1
  • 1
Zombo
  • 1
  • 62
  • 391
  • 407
  • 1
    Thanks. I didn't know about the garbage collection... that's good to know. I feel safer about running these sorts of destructive commands knowing that I can recover from them even due to user error. – Kris Anderson Apr 08 '13 at 09:07