Throw away local commits in Git

前端 未结 20 1615
遥遥无期
遥遥无期 2020-12-02 03:08

Due to some bad cherry-picking, my local Git repository is currently five commits ahead of the origin, and not in a good state. I want to get rid of all these commits and st

相关标签:
20条回答
  • 2020-12-02 03:47

    git reset --hard @{u}* deletes all your local changes on the current branch, including commits. I'm surprised no one has posted this yet considering you won't have to look up what commit to revert to or play with branches.

    * That is, reset to the current branch at @{upstream}—commonly origin/<branchname>, but not always

    0 讨论(0)
  • 2020-12-02 03:48

    I had a situation where I wanted to remove a commit that wasn't pushed, but the commit was before another one. To do so, I've used the following command

    git rebase -i HEAD~2 -> it will rebase the last two commits

    And I used 'drop' for the commit signature that I wanted to remove.

    0 讨论(0)
  • 2020-12-02 03:48

    If Your branch is ahead of 'origin/XXX' by 5 commits.

    You can issue:

    git reset --hard HEAD~5
    

    And it should remove the last 5 commits.

    0 讨论(0)
  • 2020-12-02 03:49

    For local commits which are not being pushed, you can also use git rebase -i to delete or squash a commit.

    0 讨论(0)
  • 2020-12-02 03:50

    Remove untracked files (uncommitted local changes)

    git clean -df
    

    Permanently deleting all local commits and get latest remote commit

    git reset --hard origin/<branch_name>
    
    0 讨论(0)
  • 2020-12-02 03:51

    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.

    0 讨论(0)
提交回复
热议问题