Reputation: 1529
Not sure if the title is worded right, but
I have repo-A
and repo-B
In B I have set up A and B as remotes.
Let's say B is an extension of A with some additional files and I want to periodically update B with changes from A but not push the additional B files to A
Is this possible. What is the best way to do this
Upvotes: 5
Views: 3450
Reputation: 164679
Unless there's a specific requirement to have separate repos, what you're describing is better done as a branch. B is a branch of A. Update B as any other branch with git merge A
or git rebase A
, whichever you like. From hereon out I'll assume merging. Here's what that would look like with merging.
1 - 2 - 3 - 6 - 7 - 10 - 12 - 13 [A]
\ \ \
4 - 5 - M - 8 - 9 - 11 - M - 14 [B]
If they must be separate repositories, you do the same thing but with remote branches. You might use this when you're extending a project you have no direct control over. To make things a little clearer, let's call A "upstream" and B is your repository. A is upstream of B. B only ever pulls from upstream, it never pushes back.
Set this up by first cloning A, then rename origin
(which is A) to upstream
.
git clone A
git remote rename origin upstream
Then add a new origin where you want to store your modifications remotely and push to it.
git remote add origin <url to your remote>
git push origin
Now you have this. Note that all three branches point to the same commit.
1 - 2 - 3 [master] [origin/master] [upstream/master]
Do your work normally, commit as normal, and push to origin. Here's after two new commits and a push.
1 - 2 - 3 [upstream/master]
\
4 - 5 [master] [origin/master]
When you want to update, first git fetch upstream
. This will not alter your local branches, it will only update your copies of the upstream ones. Let's say upstream's master branch has two new commits.
1 - 2 - 3 - 6 - 7 [upstream/master]
\
4 - 5 [master] [origin/master]
And then merge like any other branch: git merge upstream/master
.
1 - 2 - 3 - 6 - 7 [upstream/master]
\ \
4 - 5 - M [master] [origin/master]
You can do this in a single step as git pull upstream master
.
Continue to do this and you'll wind up with the same thing as if you'd branched.
1 - 2 - 3 - 6 - 7 - 10 - 12 - 13 [upstream/master]
\ \ \
4 - 5 - M - 8 - 9 - 11 - M - 14 [master] [origin/master]
The important things are:
git pull upstream
git push origin
Upvotes: 2
Reputation: 141956
Yes you can do it very simple:
In B I have set up A and B as remotes.
This is exactly what you had to do
You simply need to add any number for remotes to your project and use them in any way you like to. (push to 1 and pull from the other one etc).
The remotes are the urls which your repository can talk to and you decide how you wish to use them.
git remote add <origin2> <url2>
Now you can pull and merge the branches from the 2 remotes and have the code of both of them in a single repository.
As you can see in the image below you will have 2 repositories which builds up your big repository.
# clone first repository
git clone <repo1>
# add remote
git remote add <remote2> <url2>
# display the list of all the remotes
git remote -v
and I want to periodically update B with changes from A but not push the additional B files to A
All you have to do at this point is simply to pull changes from A to be
# make sure you are on the branch from repository B
git pull origin A <branch>
# now you have all the changes from A in your B branch
Upvotes: 1