将git分支挂在一个容器中

我有一个与2个不同的分支,例如master foo的git仓库:

 mkdir test && cd test git init echo master > master git add . && git commit -m "init master" git checkout -b foo rm master echo foo > foo git add . && git commit -m "init foo" 

现在,我想将容器中的masterfoo作为分离的卷装入,以便在我的容器中使用此架构:

 . ├── foo │  └── foo └── master └── master 

可能吗?

你可以使用两个git工作区 。 工作区允许您在不同的path上检出多个分支。

 $ git status On branch master … $ git worktree add ../develop Preparing ../develop (identifier develop) HEAD is now at fbbbc04 netcat6 for gnks $ git -C ../develop status On branch develop nothing to commit, working tree clean 

然后将这些目录安装到您的容器中。

你需要两个克隆,一个检出master ,另一个检出。 他们可以是浅层克隆,以节省空间。

 git clone file:///full/path/to/the/repo --depth 1 --branch master master git clone file:///full/path/to/the/repo --depth 1 --branch foo foo 

在虚拟机中只有一个克隆,并像正常的git克隆一样使用它可能更有意义。

我不相信你可以挂载git分支。 但是,如果Schern的答案是不够的(你可能想有一个公共的存储库和两个工作目录),我build议看看这个: https : //git-scm.com/docs/git-worktree

这样,两个目录将共享相同的.git目录(其中一个将是git在另一个目录中查看的指令)