7

I messed up my app and winded up doing a

git checkout <commit number>

to get back at the place where I would like to be.

when I do a git status, it says

 Your branch is behind 'origin/master' by 5 commits, and can be fast-forwarded.

I guess I'm on the 5th from my latest commit. Is there a way to make my 5th from latest commit my most recent one? pretty much to override or discard everything from my first four commits?

UPDATE.

I did a

git reset --hard <commit number>

but how can I push to my repository? it says...

To prevent you from losing history, non-fast-forward updates were rejected

I don't want to perform a merge, I pretty much want to wipe out my latest 4 commits

thanks

aerin
  • 20,607
  • 28
  • 102
  • 140
Sasha
  • 3,281
  • 7
  • 34
  • 52
  • possible duplicate of [How do you roll back (reset) a git repository to a particular commit?](http://stackoverflow.com/questions/1616957/how-do-you-roll-back-reset-a-git-repository-to-a-particular-commit) – Basilevs Apr 29 '14 at 15:53
  • Ten years ago and I'm having the same question... the timelessness of git! thx! – anthony galligani Mar 06 '23 at 16:23

3 Answers3

6

You can always force a push with the -f option:

git push -f origin master

but of course beware because this will destroy history.

Also make sure to specify both remote and branch name because without it, by default git will try and force push all branches to the given (or upstream) remote.

Gareth
  • 133,157
  • 36
  • 148
  • 157
1
git checkout ORIG_HEAD

should get you back to where you were after your most recent checkout.

If that doesn't help, you can also check out git reflog. It'll show you a list of your recent rebases, checkouts, etc., and you can find the SHA of the commit you want to get back to.

Bulwersator
  • 1,102
  • 2
  • 12
  • 30
Joe White
  • 94,807
  • 60
  • 220
  • 330
1
git reset --hard <commit number>
Gabriella Gonzalez
  • 34,863
  • 3
  • 77
  • 135
  • 1
    i resetted to my the version i want. but when i try doing git push, it says.. To prevent you from losing history, non-fast-forward updates were rejected. i pretty much want to erase the first 4 commits or undo everything – Sasha Apr 30 '12 at 01:13