Synchronizing a local Git repository with a remote one

后端 未结 10 528
独厮守ぢ
独厮守ぢ 2020-11-30 16:00

I want to synchronize my local repository with a remote one so that my local repository becomes a 100% copy of the remote one - meaning that if certain files differ in these

10条回答
  •  一向
    一向 (楼主)
    2020-11-30 16:39

    These steps will do it:

    git reset --hard HEAD
    git clean -f -x -d -n
    

    then without -n

    This will take care of all local changes. Now the commits...

    git status
    

    and note the line such as:

    Your branch is ahead of 'xxxx' by N commits.
    

    Take a note of number 'N' now:

    git reset --hard HEAD~N
    git pull
    

    and finally:

    git status
    

    should show nothing to add/commit. All clean.

    However, a fresh clone can do the same (but is much slow).

    ===Updated===

    As my git knowledge slightly improved over the the time, I have come up with yet another simpler way to do the same. Here is how (#with explanation). While in your working branch:

    git fetch # This updates 'remote' portion of local repo. 
    git reset --hard origin/
    # this will sync your local copy with remote content, discarding any committed
    # or uncommitted changes.
    

    Although your local commits and changes will disappear from sight after this, it is possible to recover committed changes, if necessary.

提交回复
热议问题