git: Your branch and 'origin/master' have diverged - how to throw away local commits?

前端 未结 6 1388
臣服心动
臣服心动 2020-12-22 17:07

I have the following message in git:

# Your branch and \'origin/master\' have diverged,
# and have 3 and 8 different commits each, respectively.
#   (use \"         


        
相关标签:
6条回答
  • 2020-12-22 17:20
    git fetch origin
    git reset --hard origin/master
    
    0 讨论(0)
  • 2020-12-22 17:25

    Try doing this to blow away your local commits:

    git reset --hard HEAD~4
    
    0 讨论(0)
  • 2020-12-22 17:30

    To erase your latest local commit use the following:

    git reset HEAD^
    

    This will get the header back to the state previous to the commit, and will allow you to git pull from master. Make sure you save all your changes elsewhere (locally) before pulling from the remote repository.

    To be able to pull without conflicts use git stash, and then git pull.

    0 讨论(0)
  • 2020-12-22 17:31

    If a hard reset doesn't cut it for you and you don't want to do a pull-merge you can throw away your local unwanted changes by deleting your local branch and re-download the origin one:

    git branch -D <local branch>
    git checkout -b <branch name> origin/<branch name>
    

    Using master as an example:

    git branch -D master
    git checkout -b master origin/master
    
    0 讨论(0)
  • 2020-12-22 17:32

    To preserve your old commits on a temporary branch in case you need them:

    git branch temp
    

    Then switch to the new master

    git fetch origin
    git reset --hard origin/master
    
    0 讨论(0)
  • 2020-12-22 17:46

    As an alternative to merging, you can rebase the feature branch onto master branch using the following commands:

    git checkout feature
    git rebase master
    
    0 讨论(0)
提交回复
热议问题