Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git, how to overwrite commits

Tags:

git

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

like image 548
Sasha Avatar asked Apr 30 '12 00:04

Sasha


2 Answers

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.

like image 80
Gareth Avatar answered Sep 29 '22 04:09

Gareth


git reset --hard <commit number>
like image 43
Gabriella Gonzalez Avatar answered Sep 29 '22 04:09

Gabriella Gonzalez