git init --separate-git-dir overwrites the existing .git folder

976 Views Asked by At

I have a repo:

mkdir test
cd test
git init
touch hi.txt
git add .
git commit -m 'first test add'
git status

Wherein I'd like to add a separate git repo to manage different work:

git init --separate-git-dir .git2

Running this command, however overwrites .git with file that stores a symlink.

I don't want this to happen :(

I'd like to have the original .git repo/folder and .git2 live in harmony...

So that I can run commands like:

git --git-dir=.git2 add new_file.txt

Thanks!

3

There are 3 best solutions below

1
emehex On BEST ANSWER

Solved the problem with:

git --work-tree=. --git-dir=git2 init

Instead of using the --separate-git-dir flag

Each subsequent command now requires:

git --work-tree=. --git-dir=git2 status
git --work-tree=. --git-dir=git2 add .
git --work-tree=. --git-dir=git2 commit -m 'yay!'

Janky. But it's exactly what I needed...

0
Enrico Campidoglio On

The purpose of the --separate-git-dir option isn't to give you multiple .git directories in the same repository, but rather to decouple the .git directory from your working tree.

From the documentation:

Instead of initializing the repository as a directory to either $GIT_DIR or ./.git/, create a text file there containing the path to the actual repository.

If this is reinitialization, the repository will be moved to the specified path.

This allows you, for example, to move the .git directory to a separate storage location (say, for space or backup reasons) while still keeping your working tree where it is.

If you want to combine multiple separate repositories (each with its own history) into one, you might want to look at submodules.

0
FishLegs On

You were using the wrong flag. --separate-git-dir flag is to store your .git folder to custom location instead of ur project's root directory.