Git - 将分支存储在单独的本地目录中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5101270/
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 - Store branches in separate local directories
提问by kbanman
I'm rather new to git, and am trying to set up my repository the right way.
我对 git 比较陌生,并且正在尝试以正确的方式设置我的存储库。
Basically my app is a platform of sorts, so implementations of this platform are based on the master branch, but have some small modifications to those files as well as some additional files.
基本上我的应用程序是一个平台,所以这个平台的实现基于主分支,但对这些文件以及一些附加文件进行了一些小的修改。
I tried setting it up as branches, so I have a master
branch, implementation_1
and implementation_2
.
我尝试将其设置为分支,所以我有一个master
分支,implementation_1
并且implementation_2
.
But as far as I can tell, that would mean that locally all the branches are stored in one directory, with their separation being only through git.
但据我所知,这意味着本地所有分支都存储在一个目录中,它们的分离仅通过 git 进行。
What I would like is to have 3 local directories, master
,imp_1
, and imp_2
. If I make a change to one of the core files in the imp_1
directory, I want to be able to merge that change into the master
branch and from there into imp_2
.
我想要的是有 3 个本地目录master
、imp_1
、 和imp_2
. 如果我对imp_1
目录中的核心文件之一进行更改,我希望能够将该更改合并到master
分支中,然后从那里合并到imp_2
.
I'm beginning to think these need to be 3 different repositories (the implementations being forks of the core). Is that the way to go? In that case, how would I go about handling the above scenario?
我开始认为这些需要是 3 个不同的存储库(实现是核心的分支)。这是要走的路吗?在这种情况下,我将如何处理上述情况?
采纳答案by VonC
Branches are first class citizens in Git. They are not "emulated" as branches like in older VCS such SVN, CVS, etc.
分支是 Git 中的一等公民。它们不像在旧的 VCS(如 SVN、CVS 等)中那样“模拟”为分支。
If you really need three different directories, because you want to have three distinct development environment, make 3 clones:
如果你真的需要三个不同的目录,因为你想要三个不同的开发环境,做3个克隆:
- one in a directory called
master
- one in a directory called
imp_1
- one in a directory called
imp_2
- 一个名为
master
- 一个名为
imp_1
- 一个名为
imp_2
But to really understand branches, you can read "Pros and cons of different branching models in DVCS".
但要真正了解分支,您可以阅读“ DVCS 中不同分支模型的优缺点”。
Or, since Git 2.5 (July 2015, 4 years after the OP's question), as I detailed in Multiple working directories with Git?, use git worktree
.
或者,从 Git 2.5(2015 年 7 月,OP 提出问题 4 年后)开始,正如我在使用 Git 的多个工作目录中详述的那样?,使用git worktree
。
That will be one clone, multiple folders (one per branch).
With Git 2.7+, you can then list those folders:
这将是一个克隆,多个文件夹(每个分支一个)。
使用 Git 2.7+,您可以列出这些文件夹:
$ git worktree list
/path/to/bare-source (bare)
/path/to/linked-worktree abcd1234 [master]
/path/to/other-linked-worktree 1234abc (detached HEAD)
回答by knittl
you can create three clones of your repository and merge between those. forks in git are no different from branches the way they are merged and such. you can easily do:
您可以创建存储库的三个克隆并在它们之间合并。git 中的 fork 与分支的合并方式等没有什么不同。你可以很容易地做到:
git merge ../master
git pull ../imp_2
when cloning locally git will hardlink your objects and even save disk space
在本地克隆时 git 将硬链接您的对象,甚至节省磁盘空间
回答by Cimbali
You can do this using git worktreethat was introduced in Git 2.5, circa July 2015.
您可以使用大约在 2015 年 7 月在 Git 2.5中引入的git worktree来完成此操作。
git clone -b master <repo> master
cd master
# checking out implementation_1 in ../imp_1
git worktree add ../imp_1 implementation_1
# creating branch implementation_2 at master~2 as you check it out in ../imp2:
git worktree add -b implementation_2 ../imp_2 master~2
And voilà! You're done.
瞧!你完成了。
With this setup, you'll have only one .git folder (in master/.git). Note that any branch can be checked out in a single worktree at a time.
通过这种设置,您将只有一个 .git 文件夹(在 master/.git 中)。请注意,可以一次在单个工作树中检出任何分支。