简体   繁体   中英

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

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.

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.

git reset --hard <commit number>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM