Can you change .git folder location

所有的git repos在工作目录内创建.git文件夹,你可以改变这个文件夹的位置。


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.

    链接地址: http://www.djcxy.com/p/18888.html

    上一篇: 将.git移动到与其管理的源不同的目录中

    下一篇: 你可以改变.git文件夹的位置