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?
3 Answers
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
-
11If 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
-
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
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
For one-time pulling
~/repo2 $ git pull </path/to/repo1> <branch> --allow-unrelated-histories
That will pull from repo1/branch to repo2/HEAD