84

I have two clones of same remote repository. I have made some changes to one local repository, how can I pull these changes to the other local repository without pushing it to the remote?

1

3 Answers 3

116

You can treat the second clone the same way you treat a remote respository on another system. You can perform all of the same operations, e.g.

~/repo1 $ git remote add repo2 ~/repo2
~/repo1 $ git fetch repo2

You'd then have all your second clone's commits in your main clone, labeled with repo2/ (just like your remote branches are probably labeled with origin/ or remote/).

If you then wanted to merge a remote branch's changes into your current local branch, you could then, e.g.:

~/repo1 $ git merge repo2/foo
3
  • 11
    If you are trying to fetch and merge in an unrelated repository, make sure to pass --allow-unrelated-histories to the merge command. Commented May 15, 2017 at 22:38
  • 8
    What is foo supposed to be here?
    – Cody
    Commented May 14, 2019 at 23:00
  • The implication here is that you may have a branch in your fetched repo2 that contained changes (perhaps that you made) that you now now to merge into repo1. You could have performed a 'git checkout repo1' just to make sure that is where your current HEAD state is. The last line would then attempt a merge from repo2/foo to repo1. You would then resolve conflicts, commit, and potentially push repo1 updates. repo2 will be unaffected because the fetch is a detached copy from the origin. Commented Jan 8, 2020 at 18:28
5

To add to djs response. After you add the local repo you can treat it as master, so all the other git commands work as normal.

For example if you have made some changes in directory A and save it in commits. To retrieve those changes in directory B (same computer) you simply open a terminal in that directory and you git pull

git pull

This will copy any changes in directory A. I use this system so that I can have a "development" environment (dir A) and a "production" environment (dir B). I only git pull in dir B

2

For one-time pulling

~/repo2 $  git pull </path/to/repo1> <branch> --allow-unrelated-histories

That will pull from repo1/branch to repo2/HEAD

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.