Git: how to work with multiple repositories?

社会主义新天地 提交于 2021-02-07 20:19:33

问题


I have a remote read-only git repository A, which I have cloned in my local filesystem. A is updated periodically, and I pull and merge the updated code in my system after each update.

I want to collaborate with my team members on the files of A, for which, I have create a new remote repository B.

How do I manage to sync B with my local repository? I would like to learn the proper command sequence and configuration options to achieve my goals. Here are the scenarios I would like to be able to solve: 1. Pull from A after there are updates, push it to B. 2. Pull from A, merge it with my local files, then push it to B, merging with the repo on B. 3. I would like all of A, my local repo and B to have identical branches.

I would be thankful for any help. If my question is not very clear, please mention it, and I will try to edit it. Thanks.


回答1:


I'm assuming that you have two remotes named A and B that have just the url configured. If you don't follow the refspecs, read the relevant chapter in the ProGit book.

1: Pull from A after there are updates, push it to B

$ # pull = fetch + (often fast-forward merge)
$ git fetch A # fetches all objects from A (all branches, commits; everything)
$ git push B 'refs/remotes/A/*:refs/heads/*'
$ # I've quoted so that the shell doesn't try to expand the *

EDIT: I've assumed that you do not want to merge changes into your local branches.

2: Pull from A, merge it with my local files, then push it to B, merging with the repo on B.

$ git pull A # fetch + attempt to merge changes in all branches
$ # Resolve any conflicts here
$ git push B '+refs/heads/*:refs/heads/*' # omit the + if it's a FF push
$ # Default is to push matching branches

EDIT: The above doesn't merge the changes in B. If you'd like to do that, you must pull changes from B also and merge them into your local branches before pushing.

3: I would like all of A, my local repo and B to have identical branches

$ git pull A
$ git checkout branchx
$ git reset --hard A/branchx # Warning! All local changes will be destroyed
$ # Repeat the above for all branches
$ git push B '+refs/heads/*:refs/heads/*' # Warning! Data in B is overwritten

EDIT: If you'd like to remove stale tracking branches, run

$ git remote prune A
$ git remote prune B



回答2:


you can try

git clone repoB url...
git remote add repoA url-to-repo-A
git pull repoA

1)

git pull repoA
git push repoB

2)

git pull repoA
git checkout local-branch-to-merge
git merge repoA/remote-branch-to-merge
git push repoB

3) I don't sure if there is any trick to checkout all the branches on repoA to repoB or to local repo with just one command... if there is none, you can always do it manually with

git checkout --track -b branch1 repoA/branch1

and when you push, they should push to the corresponding branch in remote




回答3:


I'd suggest setting up cronjob on a host which has B. This way updates from repo A are being pulled in a timely manner (say twice an hour). And you just need to pull from B.



来源:https://stackoverflow.com/questions/3124024/git-how-to-work-with-multiple-repositories

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!