nathan lachenmyer
nathan lachenmyer

Reputation: 5550

Can't revert git remote branch to an earlier commit

I was cleaning up some tracking issues, and wanted to remove some files from tracking. I screwed up and deleted a bunch of files that I want to recover. Unknowingly, I pushed to my remote in this state.

Well, time to go back and fix things. Luckily, the commit that I wanted is in the history:

black-rainbows: scottnla$ git log -2

commit 76fa0b491c2424d93b16c1fe002401eaa17d481e
Author: scottnla
Date:   Thu Oct 16 21:57:57 2014 -0400

    Fixed tracking issue

commit 078cf16ee1bebfe7130b79d5958ecb0baf855002
Author: scottnla
Date:   Thu Oct 16 21:55:50 2014 -0400

    Finished preliminary analysis script

Alright, all is not lost. So I can just use git reset --hard to get where I want, right?

black-rainbows: scottnla$ git reset --hard 

078cf16ee1bebfe7130b79d5958ecb0baf855002
HEAD is now at 078cf16 Finished preliminary analysis script

Phew. I check out the files, and this is exactly where I want things to be.

So I try to push this commit back to the remote...

black-rainbows:scottnla$ git push origin master
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to <git>
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

And now this just got really messy. I've tried different ways of pulling and merging the remote with my current branch, but it always deletes the files (as would make sense, since I just reverted history and it keeps replaying the commits).

Now that my local version of the repo is exactly how I want it, how can I push this to the remote?

Upvotes: 0

Views: 187

Answers (1)

jcm
jcm

Reputation: 1789

If you're absolutely sure that

Now that my local version of the repo is exactly how I want it, how can I push this to the remote?

then you can do

git push -f remote-name branch-name

in your case

git push -f origin master

The -f indicates that you want to forcibly push the changes. This is not a good idea if you're working with other people.

Upvotes: 2

Related Questions