68

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?

1j01
  • 2,937
  • 1
  • 21
  • 26
Sirish
  • 7,948
  • 19
  • 61
  • 97

2 Answers2

91

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
~/repo1 $ git merge repo2/foo
djs
  • 4,168
  • 1
  • 25
  • 37
  • 8
    If you are trying to fetch and merge in an unrelated repository, make sure to pass `--allow-unrelated-histories` to the merge command. – Radon Rosborough May 15 '17 at 22:38
  • 3
    What is `foo` supposed to be here? – Cody May 14 '19 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. – Robert Casey Jan 08 '20 at 18:28
2

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