简体   繁体   English

Git:两​​个子目录,如何从不同分支中拉出并推送到一个主分支

[英]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: 所以我有一个git仓库,例如mydir /,它有两个子目录:

1.)mydir/package 2.)mydir/foo. 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. mydir / package是从另一个git repo克隆而来的,我已经对其进行了设置,以便它从原始的git repo中提取并推送到个人的。 I'm mostly working out of the develop branch of this repo. 我主要在这个仓库的开发分支工作。

The mydir/foo directory is where I write personal code. 我在mydir / foo目录中编写个人代码。 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. 它使用mydir / package中的源代码,这就是为什么我将这些目录保存在一个仓库中的原因。

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). 我移到mydir / package文件夹,然后可以根据需要更新,推送和拉到正确的位置(不是正确的分支,而是正确的存储库)。 Same goes for mydir/foo. mydir / foo也是如此。

What goes wrong : If I push from mydir/, then go look at the personal repo, there are two branches; 出了什么问题 :如果我从mydir /推送,然后去查看个人仓库,有两个分支;另一个是分支。 master and develop 掌握和发展

Master: the repo has a folder /foo, which is fine. 主人:仓库有一个/ foo文件夹,很好。 But there is no /package folder 但是没有/ package文件夹

Develop: the repo has all of the contents of /package. 开发:仓库包含/ package的所有内容。 There is no /foo folder, there is no parent directory. 没有/ foo文件夹,没有父目录。

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. 我必须分别进入每个目录并进行commit / add / push / etc,然后将它们分别转到不同的分支。 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/. 我想要的是 :除了从mydir /推送外,关于此仓库的几乎所有内容都对我有用。 I want pulling from mydir/ to pull from my personal repo (so that I don't accidentally update the mydir/package folder). 我想从mydir /中提取,从我的个人仓库中提取(这样我就不会意外更新mydir / package文件夹)。 I still want pulling develop from mydir/package to pull from the original package's develop branch. 我仍然想从mydir / package中拉出develop来从原始包的develop分支中拉出。 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." 我只是想从mydir提交/添加/推送,以表示“更新个人仓库的master分支,使其现在包含mydir / package和mydir / foo的当前状态。” When I look at this personal repo, I want to see the two folders on the master branch. 当我查看此个人存储库时,我想查看master分支上的两个文件夹。

What I tried->what happened : 1.) git push remote master (from the mydir/package folder). 我试过的->发生了什么 :1.)git push remote master(来自mydir / package文件夹)。

Error ! 错误! [rejected] master -> master (fetch first) error: failed to push some refs to 'mypersonalrepo.git' [拒绝] master-> master(首先获取)错误:无法将某些引用推送到“ mypersonalrepo.git”

2.) Pull request on personal repo to merge the develop and master branches 2.)在个人仓库上拉请求以合并开发分支和主分支

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. 然后,mydir文件夹具有mydir / package(仍在develop分支上,并且进行了所有修改),mydir / foo(在右侧的分支上),然后是远程仓库的master分支中的所有文件。

3.) git add package/ -> git push origin master 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? 作为参考,如果我输入mydir / package中的git remote show origin,这是什么样子,也许我需要创建另一个远程回购来进行这种自定义?

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 ). 如果package文件夹用作foo文件夹的引用或库代码,则应将它们管理到同一分支(例如master )中。
  • If package folder works separately from foo folder, you should manage them into different branches. 如果package文件夹与foo文件夹分开工作,则应将它们管理到不同的分支中。

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. 为什么不可能:当您在master分支中将package文件夹和foo文件夹一起推送时,这意味着这两个文件夹都通过git在master分支中添加到版本控制中。 When other developers clone/pull your personal repo, package folder and foo folder will show in master branch. 当其他开发人员克隆/提取您的个人仓库时, package文件夹和foo文件夹将显示在master分支中。 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 . 即使通过其他方法,也可以为其他开发人员在master分支上仅显示foo文件夹( git fetchgit checkout origin/master -- foo ),但是工作目录的版本不是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. 因此,当开发人员将本地更改推送到您的个人存储库时,git会首先将其拉到下拉列表,并且packagefoo文件夹最终还将显示在master分支中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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