Sasha
Sasha

Reputation: 3281

git, how to overwrite commits

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

Upvotes: 7

Views: 8197

Answers (3)

Joe White
Joe White

Reputation: 97748

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.

Upvotes: 1

Gareth
Gareth

Reputation: 138082

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.

Upvotes: 6

Gabriella Gonzalez
Gabriella Gonzalez

Reputation: 35089

git reset --hard <commit number>

Upvotes: 1

Related Questions