Synchronizing a local Git repository with a remote one

后端 未结 10 458
独厮守ぢ
独厮守ぢ 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:26

    You can use git hooks for that. Just create a hook that pushes changed to the other repo after an update.

    Of course you might get merge conflicts so you have to figure how to deal with them.

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

    You need to understand that a Git repository is not just a tree of directories and files, but also stores a history of those trees - which might contain branches and merges.

    When fetching from a repository, you will copy all or some of the branches there to your repository. These are then in your repository as "remote tracking branches", e.g. branches named like remotes/origin/master or such.

    Fetching new commits from the remote repository will not change anything about your local working copy.

    Your working copy has normally a commit checked out, called HEAD. This commit is usually the tip of one of your local branches.

    I think you want to update your local branch (or maybe all the local branches?) to the corresponding remote branch, and then check out the latest branch.

    To avoid any conflicts with your working copy (which might have local changes), you first clean everything which is not versioned (using git clean). Then you check out the local branch corresponding to the remote branch you want to update to, and use git reset to switch it to the fetched remote branch. (git pull will incorporate all updates of the remote branch in your local one, which might do the same, or create a merge commit if you have local commits.)

    (But then you will really lose any local changes - both in working copy and local commits. Make sure that you really want this - otherwise better use a new branch, this saves your local commits. And use git stash to save changes which are not yet committed.)


    Edit: If you have only one local branch and are tracking one remote branch, all you need to do is

    git pull
    

    from inside the working directory.

    This will fetch the current version of all tracked remote branches and update the current branch (and the working directory) to the current version of the remote branch it is tracking.

    0 讨论(0)
  • 2020-11-30 16:28
    git fetch --prune
    

    -p, --prune
    After fetching, remove any remote-tracking branches which no longer exist on the remote. prune options

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

    Sounds like you want a mirror of the remote repository:

    git clone --mirror url://to/remote.git local.git
    

    That command creates a bare repository. If you don't want a bare repository, things get more complicated.

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

    (This info is from The Git User's Manual)

    I'm also learning, so this might not be exactly an answer to the question but it might help somebody:

    1. When a remote repository is initially cloned copies of all branches are stored in your local repository (view them with git branch -r)
    2. To update these copies and make them current (i.e. sync them with the remote branch) use git fetch. This will not effect any of you existing, custom created branches.
    3. To override your local branch checkout a fresh version of whatever branch you are working on (Assuming that you have already executed git add origin /path/to/repository) use git checkout origin/branch_name, this will override your locals changes on branch branch_name
    0 讨论(0)
  • 2020-11-30 16:37

    If you are talking about syncing a forked repo then you can follow these steps.

    How to sync a fork repository from git

    1. check your current git branch

      git branch

    2. checkout to master if you are not on master

      git checkout master

    3. Fetch the upstream repository if you have correct access rights

      git fetch upstream

    4. If you are getting below error then run

      git remote add upstream git@github.com:upstream_clone_repo_url/xyz.git

      fatal: 'upstream/master' does not appear to be a git repository  
      fatal: Could not read from remote repository.
      Please make sure you have the correct access rights and the repository exists.
      
    5. Now run the below command.

      git fetch upstream

    6. Now if you are on master then merge the upstream/master into master branch

      git merge upstream/master

      That's it!!

      Crosscheck via git remote command, more specific git remote -v

      If I also have commit rights to the upstream repo, I can create a local upstream branch and do work that will go upstream there.

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