This is common knowledge now but I’d like to make a post just so I can refer to it from other posts.
By default the .git
folder – which is your Git repository – is stored in the same folder as your other files (the “working tree”). But it is possible to separate it from the working tree if you so desire.
Two things are needed in this case: (1) the working tree must have some way of knowing where the Git repository is located, and (2) the Git repository must know where its working tree is.
The easy way to do this is to clone
or init
a repository with the --separate-git-dir /path/to/some/dir
switch. For example:
1 2 |
C:\Users\Rakhesh\Desktop>git init --separate-git-dir c:\Users\Rakhesh\Code\Test.git Test Initialized empty Git repository in c:/Users/Rakhesh/Code/Test.git/ |
The working directory “Test” only contains a file called .git
(instead of a directory called .git
). This file contains a pointer to where the actual repository is.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
C:\Users\Rakhesh\Desktop\Test>dir Volume in drive C is WINDOWS Volume Serial Number is FA14-803C Directory of C:\Users\Rakhesh\Desktop\Test 02/19/2015 02:35 PM <DIR> . 02/19/2015 02:35 PM <DIR> .. 02/19/2015 02:35 PM 39 .git 1 File(s) 39 bytes 2 Dir(s) 12,408,418,304 bytes free C:\Users\Rakhesh\Desktop\Test>more .git gitdir: c:/Users/Rakhesh/Code/Test.git |
And if you go to that folder and check the config file you will see it points to the working directory (note the core.worktree
value):
1 2 3 4 5 6 7 8 9 10 |
C:\Users\Rakhesh\Code\Test.git>more config [core] repositoryformatversion = 0 filemode = false bare = false logallrefupdates = true worktree = C:/Users/Rakhesh/Desktop/Test symlinks = false ignorecase = true hideDotFiles = dotGitOnly |
And this brings us to the non-easy way of separating the working tree from the repository. If you have an existing repository, simply move the .git
folder to wherever you want and add a .git
file as above and modify the config
file. And if you are cloning a repository, after cloning do as above. :)
A neat thing about git init
is that you can run it with the --separate-git-dir
switch on an existing working tree whose repository is within it, and it will move the repository out to wherever you specify and do the linking:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
C:\Users\Rakhesh\Desktop>git init Test Initialized empty Git repository in C:/Users/Rakhesh/Desktop/Test/.git/ C:\Users\Rakhesh\Desktop>cd Test C:\Users\Rakhesh\Desktop\Test>dir .git Volume in drive C is WINDOWS Volume Serial Number is FA14-803C Directory of C:\Users\Rakhesh\Desktop\Test\.git 02/19/2015 02:45 PM 157 config 02/19/2015 02:45 PM 73 description 02/19/2015 02:45 PM 23 HEAD 02/19/2015 02:45 PM <DIR> hooks 02/19/2015 02:45 PM <DIR> info 02/19/2015 02:45 PM <DIR> objects 02/19/2015 02:45 PM <DIR> refs 3 File(s) 253 bytes 4 Dir(s) 12,406,550,528 bytes free C:\Users\Rakhesh\Desktop\Test>git init --separate-git-dir c:\Users\Rakhesh\Code\Test.git Reinitialized existing Git repository in c:/Users/Rakhesh/Code/Test.git/ C:\Users\Rakhesh\Desktop\Test>dir Volume in drive C is WINDOWS Volume Serial Number is FA14-803C Directory of C:\Users\Rakhesh\Desktop\Test 02/19/2015 02:45 PM <DIR> . 02/19/2015 02:45 PM <DIR> .. 02/19/2015 02:45 PM 39 .git 1 File(s) 39 bytes 2 Dir(s) 12,406,550,528 bytes free |
Useful stuff to be aware of.