简体   繁体   English

在容器中挂载git分支

[英]Mount git branches in a container

I have a git repository with 2 distinct branches, for instance master and foo : 我有一个带有2个不同分支的git存储库,例如masterfoo

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"

Now I want to mount master and foo in a container as separated volumes, in order to have this architecture in my container: 现在,我想将masterfoo作为单独的卷安装在一个容器中,以便在我的容器中使用此体系结构:

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

Is it possible? 可能吗?

You could use two git worktrees . 您可以使用两个git工作树 Worktrees allow you to have more than one branch checked out at different paths. 工作树允许您在不同的路径中签出多个分支。

$ 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

Then mount those directories in your container. 然后将这些目录挂载到您的容器中。

You'd need two clones, one with master checked out and one with foo checked out. 您需要两个克隆,一个克隆已检出master ,一个克隆已检出foo They can be shallow clones to save space. 它们可以是浅克隆,以节省空间。

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

It might make more sense to just have one clone inside the VM and use it like a normal git clone. 在VM内只有一个克隆并像普通的git克隆一样使用它可能更有意义。

I don't believe you can mount git branches as-is. 我不相信您可以按原样安装git branch。 However, if Schern answer isn't enough (you may want to have a common repository and two working directories) I'd recommand looking into this: https://git-scm.com/docs/git-worktree 但是,如果Schern的答案还不够(您可能希望拥有一个公共存储库和两个工作目录),那么我建议您这样做: https : //git-scm.com/docs/git-worktree

That way, both directories will share the same .git directory (one will be an instruction for git to look in the other) 这样,两个目录将共享相同的.git目录(一个将是git在另一个目录中的指令)

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

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