Sync local git repo with remote in one shot discarding local changes/commits

后端 未结 3 464
一生所求
一生所求 2021-01-31 06:50

I\'m a not a git expert so this might look like a silly question.

I have local and remote repositories, I want to sync my local with the remote repository. I have many l

3条回答
  •  执念已碎
    2021-01-31 07:26

    As commented, a git reset --hard origin/master would reset your master to upstream.

    But: your current master (with its local commit) would be "lost" (at least, no longer visible).

    So one step isn't the best approach.

    I would recommend something like:

    git checkout master
    git fetch origin
    git branch tmp
    git reset --hard origin/master
    git checkout tmp
    git rebase master
    git checkout master
    git merge tmp
    git branch -d tmp
    

    That way, you rebase your local commit(s) on top of the updated master

    x--x--x--x--x (master, origin/master)       x--x--x--x--x--y'--y' (tmp, master)
           \                                 =>
            y--y  (tmp)                           (after rebase+merge)
    

提交回复
热议问题