简体   繁体   中英

Git: Two subdirectories, how to pull from different branches and push to one master branch

I've seen answers on how to push different directories to different branches, how to pull/push from different repos for different directories, etc. But I've got a problem combining these two things.

So I have a git repository in, say mydir/, and it has two subdirectories:

1.)mydir/package 2.)mydir/foo.

The mydir/package is cloned from another git repo, and I've set it up so that it pulls from the original git repo and pushes to a personal one. I'm mostly working out of the develop branch of this repo.

The mydir/foo directory is where I write personal code. It pulls and pushes only to the personal repo. It uses source code in mydir/package, which is why I keep these directories in one repo.

What it looks like now : It's almost fine. I move to the mydir/package folder, and I can update, push, pull as needed to the right locations (not the right branches, but the right repositories). Same goes for mydir/foo.

What goes wrong : If I push from mydir/, then go look at the personal repo, there are two branches; master and develop

Master: the repo has a folder /foo, which is fine. But there is no /package folder

Develop: the repo has all of the contents of /package. There is no /foo folder, there is no parent directory.

Why it's an issue : I've been able to use source control on everything, but it's annoying to have my pushes/changes to the two directories be on two seperate branches. I have to go into each directory seperately and commit/add/push/etc, and then each of them go to different branches. The files are backed up, which is good, but it's not practical for the next guy who tries to clone my repo.

What I want : Almost everything about this repo works for me except pushing from mydir/. I want pulling from mydir/ to pull from my personal repo (so that I don't accidentally update the mydir/package folder). I still want pulling develop from mydir/package to pull from the original package's develop branch. I just want committing/adding/pushing from mydir to mean "update the personal repo's master branch to now contain the current status of mydir/package and mydir/foo." When I look at this personal repo, I want to see the two folders on the master branch.

What I tried->what happened : 1.) git push remote master (from the mydir/package folder).

Error ! [rejected] master -> master (fetch first) error: failed to push some refs to 'mypersonalrepo.git'

2.) Pull request on personal repo to merge the develop and master branches

The mydir folder then had mydir/package (which was still on the develop branch and had all my modifications), mydir/foo (which was on the right branch), and then all of the files from the remote repo's master branch.

3.) git add package/ -> git push origin master

Nothing changed. The folders were still pushed to seperate branches of the personal repo.

For reference, this is what it looks like if I type git remote show origin from mydir/package, maybe I need to create a second remote repo to do this kind of customization?

remote origin
Fetch URL: their_package_repo.git
Push  URL: mypersonal_repo.git
HEAD branch: master
Remote branches:
develop                         tracked
master                          tracked
some_other_branch               tracked
Local branches configured for 'git pull':
  develop merges with remote develop
  master  merges with remote master
Local refs configured for 'git push':
  develop pushes to develop (fast-forwardable)
  master  pushes to master  (local out of date)

The direct answer is NO. There isn't such feature to pull branches separately and push all folders from branches to a branch.

  • If the package folder works as reference or library code for foo folder, you should manage them into the same branch (such as master ).
  • If package folder works separately from foo folder, you should manage them into different branches.

Why it's impossible: when you push package folder and foo folder together in master branch, that means the two folders are both add to version control by git in master branch. When other developers clone/pull your personal repo, package folder and foo folder will show in master branch. Even through there has other ways to show only foo folder on master branch for other developers ( git fetch and git checkout origin/master -- foo ), but the working directory is not the version as origin/master . So when the developers push their local changes to your personal repo, git will hit them to pull first, and the package and foo folder will also shows in master branch eventually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM