git discard all changes and pull from upstream

前端 未结 5 1089
迷失自我
迷失自我 2021-01-29 17:59

How do I fetch upstream repo and make it replace master? I only have one branch on my repo, which is master, and I completely messed it up, so I basically need to start over fro

相关标签:
5条回答
  • 2021-01-29 18:33

    There are (at least) two things you can do here–you can reclone the remote repo, or you can reset --hard to the common ancestor and then do a pull, which will fast-forward to the latest commit on the remote master.

    To be concrete, here's a simple extension of Nevik Rehnel's original answer:

    git reset --hard origin/master
    git pull origin master
    

    NOTE: using git reset --hard will discard any uncommitted changes, and it can be easy to confuse yourself with this command if you're new to git, so make sure you have a sense of what it is going to do before proceeding.

    0 讨论(0)
  • 2021-01-29 18:34

    while on branch master: git reset --hard origin/master

    then do some clean up with git gc (more about this in the man pages)

    Update: You will also probably need to do a git fetch origin (or git fetch origin master if you only want that branch); it should not matter if you do this before or after the reset. (Thanks @eric-walker)

    0 讨论(0)
  • 2021-01-29 18:37

    You can do it in a single command:

    git fetch --all && git reset --hard origin/master
    

    Or in a pair of commands:

    git fetch --all
    git reset --hard origin/master
    

    Note than you will lose ALL your local changes

    0 讨论(0)
  • 2021-01-29 18:44
    git reset <hash>  # you need to know the last good hash, so you can remove all your local commits
    
    git fetch upstream
    git checkout master
    git merge upstream/master
    git push origin master -f
    

    voila, now your fork is back to same as upstream.

    0 讨论(0)
  • 2021-01-29 18:49

    I finally realized now that instead of

    git fetch --all && git reset --hard origin/master
    

    it should be

    git fetch --all && git reset --hard origin/<branch_name>
    

    instead (if one works on a different branch)

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