All git repos create .git folder inside working directory. Can you change this folder location?
-
No it's necessary to set the git repository. – Jepessen Nov 12 '16 at 09:02
-
2As in http://stackoverflow.com/q/505467/6309? – VonC Nov 12 '16 at 09:03
-
Moving the folder would change the scope of which folders are under Git's version control. But you could move it to a new location and reinit from there. – Tim Biegeleisen Nov 12 '16 at 09:04
3 Answers
You can tell Git to create the .git
directory somewhere else when you create the repository:
git init --separate-git-dir=/path/to/dot-git-directory .
git init
is used to create a new local repository but you can also run git init
in an existing repository to re-initialize some of its properties. The location of the .git
directory is one of them.
The documentation says:
Running
git init
in an existing repository is safe. It will not overwrite things that are already there. The primary reason for rerunning git init is to pick up newly added templates (or to move the repository to another place if--separate-git-dir
is given).
Git renames the .git
directory using the name provided as argument to the --separate-git-dir
option. In the working directory it places a file named .git
instead of the .git
directory. The .git
file contains the location of the actual .git
directory.
If you want to put the .git
directory back in the working directory all you have to do is to remove the .git
file and put the .git
directory instead. git init --separate-git-dir
apparently cannot be used for this.
If you don't want to have the .git
directory or a .git
file in your working directory at all, then there are two solutions but both are inconvenient:
- pass the
--git-dir=/path/to/dot-git-directory
argument to all Git commands; - set the environment variable
GIT_DIR=/path/to/dot-git-directory
.
The first option requires a lot of typing and your Git commands become long and difficult to read.
The second option makes it difficult to work with multiple repositories. Every time you want to work in a different repository you have to remember to set the $GIT_DIR
environment variable with the correct path; otherwise you will mess the things up.

- 68,258
- 9
- 99
- 134
-
Awesome! Can I hack git so the `.git` file be also in another folder? Like one folder deeper, an set the directory to say a upper folder. Off course then I would have to run commands in the `.git` folder, probably.. – Bernardo Dal Corno Aug 28 '17 at 19:07
-
When I use `--separate-git-dir` I don't find any .git file created to replace the .git folder, so actually that does the job to have no .git in your working directory doesn't it ? – lapin Nov 22 '17 at 08:59
-
@lapin If you cannot find it this doesn't mean it is not created. Read the docs: https://git-scm.com/docs/git-init#git-init---separate-git-dirltgitdirgt – axiac Nov 22 '17 at 09:49
-
This is what aliases are for. `alias git="/usr/bin/git --separate-git-dir
"` and if there are some instances where you don't want the option to be automatically added, `alias gitv="/usr/bin/git"` for vanilla version. I know that git has an alias subcommand, but I don't know that you could invoke it on its own self. – Nate T Jan 17 '22 at 04:47
Here is the trick for me to re-establish from a given .git dir.
Move the .git dir to your desired dir. If you 'git status' in the new dir, you will see everything is in deleted state. The trick is to 'git stash' all the deletion away, at the same time, everything will be automatically re-installed/moved out of the .git dir. So, the new dir is a completely new git working repository.

- 61
- 3
Following this answer in case you want to do this manually (i.e. not using git init --separate-git-dir
). I'll assume that in the directory D:\projectfolder
there are your files and a .git folder. You want to move that folder to D:\gitfolder
In D:\projectfolder
create a file (not folder) named .git
with this content:
gitdir: D:\gitfolder\.git
In folder D:\gitfolder\.git
edit the config file with this line under [core]
:
worktree = D:\projectfolder
You can also do this with this command git config core.worktree D:\projectfolder
.

- 1,969
- 4
- 28
- 58