Throw away local commits in Git

前端 未结 20 1617
遥遥无期
遥遥无期 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:53

    Simply delete your local master branch and recreate it like so:

    git branch -D master
    git checkout origin/master -b master
    
    0 讨论(0)
  • 2020-12-02 03:53

    Delete the most recent commit:

    git reset --hard HEAD~1

    Delete the most recent commit, without destroying the work you've done:

    git reset --soft HEAD~1

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

    Before answering let's add some background, explaining what is this HEAD. since some of the options below will result in detached head

    First of all what is HEAD?

    HEAD is simply a reference to the current commit (latest) on the current branch.
    There can only be a single HEAD at any given time. (excluding git worktree)

    The content of HEAD is stored inside .git/HEAD and it contains the 40 bytes SHA-1 of the current commit.


    detached HEAD

    If you are not on the latest commit - meaning that HEAD is pointing to a prior commit in history its called detached HEAD.

    On the command line, it will look like this- SHA-1 instead of the branch name since the HEAD is not pointing to the tip of the current branch

    A few options on how to recover from a detached HEAD:


    git checkout

    git checkout <commit_id>
    git checkout -b <new branch> <commit_id>
    git checkout HEAD~X // x is the number of commits t go back
    

    This will checkout new branch pointing to the desired commit.
    This command will checkout to a given commit.
    At this point, you can create a branch and start to work from this point on.

    # Checkout a given commit. 
    # Doing so will result in a `detached HEAD` which mean that the `HEAD`
    # is not pointing to the latest so you will need to checkout branch
    # in order to be able to update the code.
    git checkout <commit-id>
    
    # create a new branch forked to the given commit
    git checkout -b <branch name>
    

    git reflog

    You can always use the reflog as well.
    git reflog will display any change which updated the HEAD and checking out the desired reflog entry will set the HEAD back to this commit.

    Every time the HEAD is modified there will be a new entry in the reflog

    git reflog
    git checkout HEAD@{...}
    

    This will get you back to your desired commit


    git reset --hard <commit_id>

    "Move" your HEAD back to the desired commit.

    # This will destroy any local modifications.
    # Don't do it if you have uncommitted work you want to keep.
    git reset --hard 0d1d7fc32
    
    # Alternatively, if there's work to keep:
    git stash
    git reset --hard 0d1d7fc32
    git stash pop
    # This saves the modifications, then reapplies that patch after resetting.
    # You could get merge conflicts if you've modified things which were
    # changed since the commit you reset to.
    
    • Note: (Since Git 2.7)
      you can also use the git rebase --no-autostash as well.

    git revert <sha-1>

    "Undo" the given commit or commit range.
    The reset command will "undo" any changes made in the given commit.
    A new commit with the undo patch will be committed while the original commit will remain in the history as well.

    # add new commit with the undo of the original one.
    # the <sha-1> can be any commit(s) or commit range
    git revert <sha-1>
    

    This schema illustrates which command does what.
    As you can see there reset && checkout modify the HEAD.

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

    If you just want to throw away local commits and keep the modifications done in files then do
    git reset @~
    Other answers addressed the hard reset

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

    Use any number of times, to revert back to the last commit without deleting any files that you have recently created.

    git reset --soft HEAD~1
    

    Then use

    git reset HEAD <name-of-file/files*>
    

    to unstage, or untrack.

    0 讨论(0)
  • 2020-12-02 04:00

    To see/get the SHA-1 id of the commit you want to come back too

    gitk --all
    

    To roll back to that commit

    git reset --hard sha1_id
    

    !Note. All the commits that were made after that commit will be deleted (and all your modification to the project). So first better clone the project to another branch or copy to another directory.

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