Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you change .git folder location

Tags:

git

All git repos create .git folder inside working directory. Can you change this folder location?

like image 575
Jatin Gera Avatar asked Nov 12 '16 09:11

Jatin Gera


2 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:

  1. pass the --git-dir=/path/to/dot-git-directory argument to all Git commands;
  2. 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.

like image 87
axiac Avatar answered Sep 29 '22 22:09

axiac


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.

like image 37
user3179473 Avatar answered Sep 29 '22 22:09

user3179473