chz
chz

Reputation: 387

Git clone multiple branches into same local directory possible?

we have 2 separate repos but they need to be in the same local directory to compile our code properly.

Can one git clone separately into the same local directory without any side effects ?

git clone [email protected]:app/z.git -b b1 local_dir
git clone [email protected]:app/y.git -b b1 local_dir

Thank you.

Upvotes: 2

Views: 3679

Answers (3)

VonC
VonC

Reputation: 1324935

Once you clone a git repo, you will be able to checkout multiple branches in different path with git worktree add <path> [<branch>], coming with Git 2.5+ (July 2015).

That replaces an older script contrib/workdir/git-new-workdir, with a more robust mechanism where those "linked" working trees are actually recorded in the main repo new $GIT_DIR/worktrees folder (so that work on any OS, including Windows).

Again, once you have cloned a repo (in a folder like /path/to/myrepo), you can checkout different branches in different independent paths (/path/to/br1, /path/to/br2), while having those working trees linked to the main repo history (no need to use a --git-dir option anymore)

See more at "Multiple working directories with Git?"

Upvotes: 1

Roman
Roman

Reputation: 20246

You may be able to do this by specifying the git-dir or working-tree as described in this answer. Your repositories will live in different directories but share the working folder allowing you to mix the files to compile.

However, you likely only be able to do this once and then essentially not be able to do any other git operations in a meaningful manner. Each repo should see the other repo's files as untracked so your working directory will always be dirty. You should still be able to commit, but any pull would require you to stash and unstash all of the untracked files. If it works, it will be a huge pain.

On a personal note, if this is indeed how the projects are supposed to be compiled, I recommend punching the person who set it up.

Upvotes: 1

John Zwinck
John Zwinck

Reputation: 249273

No, you cannot have two Git repositories exist in the same directory.

One reason for this is that each Git repo has a .git subdirectory inside. If there are two repos in one directory, their .git subdirectories will collide.

Another reason is that it's unclear what should happen if both repos contain a file at the same relative location.

You can have Git repos inside of other Git repos (see git help submodule), but that's not quite what you're looking for.

You should probably look for an alternate solution for the higher-level problem to "compile the code properly." But if you're really stuck and out of easier ideas, you could consider making a directory of symbolic links which point to the required files in the two separate Git repos. That directory of links could even be checked in, either as a third repo or within one of the existing two.

Upvotes: 2

Related Questions