Synchronizing a local Git repository with a remote one

后端 未结 10 459
独厮守ぢ
独厮守ぢ 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/<your-working-branch>
    # 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.

    0 讨论(0)
  • 2020-11-30 16:49

    The permanent fix if one wants to create a new branch on the remote to mirror and track your local branch(or, vice-versa) is:

    git config --global push.default current
    

    I always configure my local git with this command after I do git clone. Although it can be applied anytime when the local-remote branch "Git fatal: The current branch has no upstream branch" error occurs.

    Hope this helps. Much peace. :)

    0 讨论(0)
  • 2020-11-30 16:52

    You want to do

    git fetch --prune origin
    git reset --hard origin/master
    git clean -f -d
    

    This makes your local repo exactly like your remote repo.

    Remember to replace origin and master with the remote and branch that you want to synchronize with.

    0 讨论(0)
  • 2020-11-30 16:53

    Reset and sync local repository with remote branch

    The command: Remember to replace origin and master with the remote and branch that you want to synchronize with.

    git fetch origin && git reset --hard origin/master && git clean -f -d
    

    Or step-by-step:

    git fetch origin
    git reset --hard origin/master
    git clean -f -d
    

    Your local branch is now an exact copy (commits and all) of the remote branch.

    Command output:

    Here is an example of running the command on a local clone of the Forge a git repository.

    sharkbook:forge lbaxter$ git fetch origin && git reset --hard origin/master && git clean -f -d
    HEAD is now at 356cd85 FORGE-680
    Removing forge-example-plugin/
    Removing plugin-container-api/
    Removing plugin-container/
    Removing shell/.forge_settings
    sharkbook:forge lbaxter$
    
    0 讨论(0)
提交回复
热议问题