Git - 每个分支的单独文件夹。设置它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15030975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git - separate folder for each branch. Setting it up
提问by Kevin Rave
I have a need to keep 3 branches in 3 separate folders. (I know this is not a git way of doing things. But I need to do this for a reason).
我需要在 3 个单独的文件夹中保留 3 个分支。(我知道这不是 git 的做事方式。但我需要这样做是有原因的)。
Lets say the repo name is my_proj_repo.git
I have created a folder called prodv1
in my local system:
假设 repo 名称是my_proj_repo.git
我prodv1
在本地系统中创建了一个文件夹:
git clone url:/my_proj_repo.git
Now I went into prodv1
folder and copied the files from a server, then:
现在我进入prodv1
文件夹并从服务器复制文件,然后:
git commit -am "initial import"
git push origin master
That pushed the files to master. Now I created two more folders like the above in my local system
这推动了文件掌握。现在我在本地系统中创建了另外两个像上面这样的文件夹
stagingv1
devv1
I want to create two local and remote branches with the names:
我想创建两个具有以下名称的本地和远程分支:
staging // this local branch points (push/pull) to staging remote branch
dev // this local branch points ((push/pull) to dev remote branch
And these two branches should be in the corresponding folders:
而这两个分支应该在对应的文件夹中:
staging ==> stagingv1 folder
dev ==> devv1 folder
I tried a few things and I guess I messed it up. Can someone point me commands/steps to get all this setup like I wanted?
我尝试了一些东西,我想我搞砸了。有人可以指出我想要的命令/步骤来获得所有这些设置吗?
What I did was, went into the stagingv1
folder and did a git clone <repo>
. But it defaults to master
. And I am not sure how to initialize the staging
branch here.
我所做的是,进入stagingv1
文件夹并做了一个git clone <repo>
. 但它默认为master
. 而且我不确定如何在staging
这里初始化分支。
回答by JudgeProphet
I think that you might have to create all the branch in your repo
我认为您可能必须在您的回购中创建所有分支
$ git branch <name_of_your_new_branch>
Then create all your folders and in each of your folder Clone the repo but Checkout the appropriate branches.
然后创建您的所有文件夹,并在您的每个文件夹中克隆 repo 但签出相应的分支。
Each folder should / may track only the appropriate branch
每个文件夹应该/可以只跟踪适当的分支
Step 1 $ git branch [name_branch#1]
Step 2 $ git branch [name_branch#2]
Step 3 $ git branch [name_branch#3]
...
...
Step 4 $ git push --all
Step 5 md Folder #2
Step 6 $ git clone [URL]
Step 7 $ git checkout [name_branch]
回答by Cimbali
What you want to achieve has become simpler (or even trivial) since git 2.5 introduced the git worktreecommand.
自从 git 2.5 引入git worktree命令以来,您想要实现的目标变得更简单(甚至微不足道)。
Basically, your your git repo has now completely free number of checked out branches, called work tree:
基本上,您的 git 存储库现在拥有完全免费数量的签出分支,称为工作树:
- 0 (a bare repo)
- 1 (a normal repo)
- n > 1, a normal repo where you added n-1 work trees
- 0(一个裸仓库)
- 1(一个普通的回购)
- n > 1,一个普通的 repo,你在其中添加了 n-1 个工作树
If your repo already contains your branch, you can do:git worktree add <path to branch> <branch name>
Or, if that branch isn't created yet and you want to branch off master:git worktree add -b <new branch name> <path to branch> master
如果您的 repo 已经包含您的分支,您可以执行以下操作:git worktree add <path to branch> <branch name>
或者,如果尚未创建该分支并且您想从 master 分支:git worktree add -b <new branch name> <path to branch> master
There you go. Note that you can't checkout a branch in several repositories simultaneously.
你去吧。请注意,您不能同时检出多个存储库中的分支。
回答by Jassi
The above scenario as put up in the question, is pretty much valid in following scenarios.
问题中提出的上述场景在以下场景中非常有效。
Micro-services: AWS Lambda development for example one or two develop work on an lambda, there are say 70 lambdas total. Thus, 70 folders including test and code_folder. If it was a monolithic system, you have to make sure all parts of system work together by having zero merge issue. But here each of 70 folder is independent except few global configuration and few shared folders.
Monorepo: Large companies use monorepo to manage all sub-projects of a larger project including mobile app, websites, backend and other related stuff.
微服务:AWS Lambda 开发,例如一两个在 lambda 上的开发工作,总共有 70 个 lambda。因此,包括 test 和 code_folder 在内的 70 个文件夹。如果它是一个整体系统,您必须通过零合并问题来确保系统的所有部分协同工作。但是这里70个文件夹中的每一个都是独立的,除了很少的全局配置和很少的共享文件夹。
Monorepo:大公司使用 monorepo 来管理大型项目的所有子项目,包括移动应用程序、网站、后端和其他相关内容。
Master branch may or may not have all the code, if each branch track only changes in its folder.
如果每个分支仅跟踪其文件夹中的更改,则主分支可能包含也可能不包含所有代码。
One way to do it, first create a shared branch for shared modules. Then, create multiple branches where each branch starts with empty folder. Never merge your branch with master, only pull shared branch merge your branch locally and push to remote branch.
一种方法是,首先为共享模块创建一个共享分支。然后,创建多个分支,每个分支都以空文件夹开头。永远不要将您的分支与 master 合并,只拉共享分支在本地合并您的分支并推送到远程分支。
Thus, every branch on remote has shared modules and your code.
因此,远程上的每个分支都有共享模块和您的代码。