Throw away local commits in git

Due to some bad cherry-picking, my local git repository is currently 5 commits ahead of the origin, and not in a good state. I want to get rid of all these commits and start over again.

Obviously, deleting my working directory and re-cloning would do it, but downloading everything from github again seems like overkill, and not a good use of my time.

Maybe git revert is what I need, but I don't want to end up 10 commits ahead of the origin (or even 6), even if it does get the code itself back to the right state. I just want to pretend the last half-hour never happened.

Is there a simple command that will do this? It seems like an obvious use case, but I'm not finding any examples of it.


Note that this question is specifically about commits, not about:

  • untracked files
  • unstaged changes
  • staged but uncommitted changes

  • If your excess commits are only visible to you, you can just do git reset --hard origin/<branch_name> to move back to where the origin is.

    Doing a git revert makes new commits to remove old commits in a way that keeps everyone's history sane.


    只需删除您的本地主分支并重新创建它就像这样:

    git branch -D master
    git checkout origin/master -b master
    

    Try:

    git reset --hard <the sha1 hash>
    

    to reset your head to wherever you want to be. Use gitk to see which commit you want to be at. You can do reset within gitk as well.

    链接地址: http://www.djcxy.com/p/4036.html

    上一篇: 如何用远程主人重新绑定本地分支

    下一篇: 扔掉git中的本地提交