简体   繁体   中英

Mount git branches in a container

I have a git repository with 2 distinct branches, for instance master and foo :

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:

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

Is it possible?

You could use two git worktrees . 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. 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.

I don't believe you can mount git branches as-is. 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

That way, both directories will share the same .git directory (one will be an instruction for git to look in the other)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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