0

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!

Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
emehex
  • 9,874
  • 10
  • 54
  • 100
  • 2
    For this you need to store the second git dir outside the repository root. But what is the actual problem you're trying to solve? As it's written now, your questions sounds my [XY-problem](http://xyproblem.info/) signals. – fredrik Mar 02 '20 at 12:09

3 Answers3

3

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.

Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
0

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...

emehex
  • 9,874
  • 10
  • 54
  • 100
  • 1
    Your solution seems to work for me, but how do you manage the .gitignore file? I tried to put it in the work directory, or in the git dir using contextual or full path and none wokred. – Processor Jun 30 '22 at 17:06
0

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.

FishLegs
  • 221
  • 3
  • 15