9

I accidently ran:

git push origin +master

while trying to push a file to my Github repo. This command reset all the commit history and deleted a few files.

I was wondering if there is a way to reset my repo to a certain commit having the hash 94b90dc1121ce477131fa60ffdc234591554b6c8.

jkdev
  • 11,360
  • 15
  • 54
  • 77
user2821370
  • 124
  • 1
  • 8
  • 1
    I never knew about that `+` modifier. I'd think that push using a refspec that would cause remote commits to become unreferenced would need a `-f` option before it would be applied. – Michael Burr Dec 03 '15 at 01:27
  • Possible duplicate of [Reset or revert a specific file to a specific revision using Git?](http://stackoverflow.com/questions/215718/reset-or-revert-a-specific-file-to-a-specific-revision-using-git) – ErTR Dec 03 '15 at 03:11

2 Answers2

12
git checkout master
git reset --hard 94b90dc1121ce477131fa60ffdc234591554b6c8
git push -f origin master

Here's an explanation of the commmands...

First, let's make sure you are on the master branch, so use the checkout command so your HEAD points to master.

What is HEAD? It's basically the pointer that Git maintains to point to where you currently are in the Git tree.

In fact, most of these concepts like HEAD, and branches, are just pointers to different points in the tree. Do a gitk from the command line to see the tree in a nice graphical format.

Next, let's reset your HEAD pointer to that exact commit that you had in your original question. This will basically make the files on your file-system match that commit.

Be careful will reset --hard though...if you have outstanding work, or commit's that are not-yet pushed to the server this may make them "unreachable" (think about that tree again)...if you "lose" commits from this command you can usually get them back by using the reflog though.

Finally, push the local state of your master branch to GitHub's master branch. The -f is there because you are re-writing the history of the branch, so you need to tell Git to "force" it.

Jonathan.Brink
  • 23,757
  • 20
  • 73
  • 115
3

Jonathan's answer is correct. An alternative way to achieve the same is:

git push -f origin 94b90dc1121ce477131fa60ffdc234591554b6c8:master
Ayush
  • 41,754
  • 51
  • 164
  • 239
  • Is this syntactic sugar for the Jonathan's answer, or does it work differently internally? – k0pernikus Dec 03 '15 at 01:00
  • Sorry I didn't try out your method as Jonathan's already worked, thank you very much anyways! – user2821370 Dec 03 '15 at 01:01
  • 1
    @k0pernikus: It's syntactic sugar mostly. Jonathan first sets his local 'master' to the correct commit and then force pushes it to origin/master, while this force pushes the ref to origin/master directly without setting the local 'master' branch first. – Ayush Dec 03 '15 at 01:09
  • So from a micro-optimization point of view this would be a tiny bit faster, as it doesn't change to local git repo. (How that is useful may be a different matter, as I guess one wants the local repo to reflect the origin anyway.) Wouldn't `git pull` using this approach try a merge commit? – k0pernikus Dec 03 '15 at 01:20