This variable is intended to support multiple working directories
attached to a repository. Such a repository may have a main working
directory, created by either "git init" or "git clone" and one or more
linked working directories. These working directories and the main
repository share the same repository directory.

In linked working directories, $GIT_COMMON_DIR must be defined to point
to the real repository directory and $GIT_DIR points to an unused
subdirectory inside $GIT_COMMON_DIR. File locations inside the
repository are reorganized from the linked worktree view point:

 - worktree-specific such as HEAD, logs/HEAD, index, other top-level
   refs and unrecognized files are from $GIT_DIR.

 - the rest like objects, refs, info, hooks, packed-refs, shallow...
   are from $GIT_COMMON_DIR

Scripts are supposed to retrieve paths in $GIT_DIR with "git rev-parse
--git-path", which will take care of "$GIT_DIR vs $GIT_COMMON_DIR"

The redirection is done by git_path(), git_pathdup() and
strbuf_git_path(). The selected list of paths goes to $GIT_COMMON_DIR,
not the other way around in case a developer adds a new
worktree-specific file and it's accidentally promoted to be shared
across repositories (this includes unknown files added by third party

The list of known files that belong to $GIT_DIR are:

next-index-* rebase-apply rebase-merge rsync-refs-* sequencer/*

Path mapping is NOT done for git_path_submodule(). Multi-checkouts are
not supported as submodules.

Signed-off-by: Nguyễn Thái Ngọc Duy <>
 Documentation/git.txt                  |  8 +++++++
 Documentation/gitrepository-layout.txt | 42 ++++++++++++++++++++++++++--------
 cache.h                                |  4 +++-
 environment.c                          | 19 +++++++++++----
 path.c                                 | 34 +++++++++++++++++++++++++++
 t/                  | 15 ++++++++++++
 6 files changed, 108 insertions(+), 14 deletions(-)

diff --git a/Documentation/git.txt b/Documentation/git.txt
index 02bbc08..b094b1f 100644
--- a/Documentation/git.txt
+++ b/Documentation/git.txt
@@ -773,6 +773,14 @@ Git so take care if using Cogito etc.
        an explicit repository directory set via 'GIT_DIR' or on the
        command line.
+       If this variable is set to a path, non-worktree files that are
+       normally in $GIT_DIR will be taken from this path
+       instead. Worktree-specific files such as HEAD or index are
+       taken from $GIT_DIR. See linkgit:gitrepository-layout[5] for
+       details. This variable has lower precedence than other path
+       variables such as GIT_INDEX_FILE, GIT_OBJECT_DIRECTORY...
 Git Commits
diff --git a/Documentation/gitrepository-layout.txt 
index aa03882..556da09 100644
--- a/Documentation/gitrepository-layout.txt
+++ b/Documentation/gitrepository-layout.txt
@@ -46,6 +46,9 @@ of incomplete object store is not suitable to be published for
 use with dumb transports but otherwise is OK as long as
 `objects/info/alternates` points at the object stores it
 borrows from.
+This directory is ignored if $GIT_COMMON_DIR is set and
+"$GIT_COMMON_DIR/objects" will be used instead.
        A newly created object is stored in its own file.
@@ -92,7 +95,8 @@ refs::
        References are stored in subdirectories of this
        directory.  The 'git prune' command knows to preserve
        objects reachable from refs found in this directory and
-       its subdirectories.
+       its subdirectories. This directory is ignored if $GIT_COMMON_DIR
+       is set and "$GIT_COMMON_DIR/refs" will be used instead.
        records tip-of-the-tree commit objects of branch `name`
@@ -114,7 +118,8 @@ refs/replace/`<obj-sha1>`::
        records the same information as refs/heads/, refs/tags/,
        and friends record in a more efficient way.  See
-       linkgit:git-pack-refs[1].
+       linkgit:git-pack-refs[1]. This file is ignored if $GIT_COMMON_DIR
+       is set and "$GIT_COMMON_DIR/packed-refs" will be used instead.
        A symref (see glossary) to the `refs/heads/` namespace
@@ -133,6 +138,11 @@ being a symref to point at the current branch.  Such a 
 is often called 'detached HEAD.'  See linkgit:git-checkout[1]
 for details.
+       Repository specific configuration file. This file is ignored
+       $GIT_COMMON_DIR is set and "$GIT_COMMON_DIR/config" will be
+       used instead.
        A slightly deprecated way to store shorthands to be used
        to specify a URL to 'git fetch', 'git pull' and 'git push'.
@@ -140,7 +150,10 @@ branches::
        'name' can be given to these commands in place of
        'repository' argument.  See the REMOTES section in
        linkgit:git-fetch[1] for details.  This mechanism is legacy
-       and not likely to be found in modern repositories.
+       and not likely to be found in modern repositories. This
+       directory is ignored if $GIT_COMMON_DIR is set and
+       "$GIT_COMMON_DIR/branches" will be used instead.
        Hooks are customization scripts used by various Git
@@ -149,7 +162,9 @@ hooks::
        default.  To enable, the `.sample` suffix has to be
        removed from the filename by renaming.
        Read linkgit:githooks[5] for more details about
-       each hook.
+       each hook. This directory is ignored if $GIT_COMMON_DIR is set
+       and "$GIT_COMMON_DIR/hooks" will be used instead.
        The current index file for the repository.  It is
@@ -157,7 +172,8 @@ index::
        Additional information about the repository is recorded
-       in this directory.
+       in this directory. This directory is ignored if $GIT_COMMON_DIR
+       is set and "$GIT_COMMON_DIR/index" will be used instead.
        This file helps dumb transports discover what refs are
@@ -193,12 +209,16 @@ remotes::
        when interacting with remote repositories via 'git fetch',
        'git pull' and 'git push' commands.  See the REMOTES section
        in linkgit:git-fetch[1] for details.  This mechanism is legacy
-       and not likely to be found in modern repositories.
+       and not likely to be found in modern repositories. This
+       directory is ignored if $GIT_COMMON_DIR is set and
+       "$GIT_COMMON_DIR/remotes" will be used instead.
        Records of changes made to refs are stored in this
        directory.  See linkgit:git-update-ref[1]
-       for more information.
+       for more information. This directory is ignored
+       $GIT_COMMON_DIR is set and "$GIT_COMMON_DIR/logs" will be used
+       instead.
        Records all changes made to the branch tip named `name`.
@@ -209,10 +229,14 @@ logs/refs/tags/`name`::
        This is similar to `info/grafts` but is internally used
        and maintained by shallow clone mechanism.  See `--depth`
-       option to linkgit:git-clone[1] and linkgit:git-fetch[1].
+       option to linkgit:git-clone[1] and linkgit:git-fetch[1]. This
+       file is ignored if $GIT_COMMON_DIR is set and
+       "$GIT_COMMON_DIR/shallow" will be used instead.
-       Contains the git-repositories of the submodules.
+       Contains the git-repositories of the submodules. This
+       directory is ignored if $GIT_COMMON_DIR is set and
+       "$GIT_COMMON_DIR/modules" will be used instead.
diff --git a/cache.h b/cache.h
index 0f5f44f..9182427 100644
--- a/cache.h
+++ b/cache.h
@@ -347,6 +347,7 @@ static inline enum object_type object_type(unsigned int 
 /* Double-check local_repo_env below if you add to this list. */
@@ -400,6 +401,7 @@ extern int is_inside_git_dir(void);
 extern char *git_work_tree_cfg;
 extern int is_inside_work_tree(void);
 extern const char *get_git_dir(void);
+extern const char *get_git_common_dir(void);
 extern int is_git_directory(const char *path);
 extern char *get_object_directory(void);
 extern char *get_index_file(void);
@@ -585,7 +587,7 @@ extern int fsync_object_files;
 extern int core_preload_index;
 extern int core_apply_sparse_checkout;
 extern int precomposed_unicode;
-extern int git_db_env, git_index_env, git_graft_env;
+extern int git_db_env, git_index_env, git_graft_env, git_common_dir_env;
  * The character that begins a commented line in user-editable file
diff --git a/environment.c b/environment.c
index f513479..c998120 100644
--- a/environment.c
+++ b/environment.c
@@ -80,9 +80,9 @@ static char *work_tree;
 static const char *namespace;
 static size_t namespace_len;
-static const char *git_dir;
+static const char *git_dir, *git_common_dir;
 static char *git_object_dir, *git_index_file, *git_graft_file;
-int git_db_env, git_index_env, git_graft_env;
+int git_db_env, git_index_env, git_graft_env, git_common_dir_env;
  * Repository-local GIT_* environment variables; see cache.h for details.
@@ -134,10 +134,16 @@ static void setup_git_env(void)
                git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
        gitfile = read_gitfile(git_dir);
        git_dir = xstrdup(gitfile ? gitfile : git_dir);
+       git_common_dir = getenv(GIT_COMMON_DIR_ENVIRONMENT);
+       if (git_common_dir) {
+               git_common_dir_env = 1;
+               git_common_dir = xstrdup(git_common_dir);
+       } else
+               git_common_dir = git_dir;
        git_object_dir = getenv(DB_ENVIRONMENT);
        if (!git_object_dir) {
-               git_object_dir = xmalloc(strlen(git_dir) + 9);
-               sprintf(git_object_dir, "%s/objects", git_dir);
+               git_object_dir = xmalloc(strlen(git_common_dir) + 9);
+               sprintf(git_object_dir, "%s/objects", git_common_dir);
        } else
                git_db_env = 1;
        git_index_file = getenv(INDEX_ENVIRONMENT);
@@ -173,6 +179,11 @@ const char *get_git_dir(void)
        return git_dir;
+const char *get_git_common_dir(void)
+       return git_common_dir;
 const char *get_git_namespace(void)
        if (!namespace)
diff --git a/path.c b/path.c
index 1069ae0..8453687 100644
--- a/path.c
+++ b/path.c
@@ -90,6 +90,38 @@ static void replace_dir(struct strbuf *buf, int len, const 
char *newdir)
                buf->buf[newlen] = '/';
+static const char *common_list[] = {
+       "/branches", "/hooks", "/info", "/logs", "/lost-found", "/modules",
+       "/objects", "/refs", "/remotes", "/rr-cache", "/svn",
+       "config", "", "packed-refs", "shallow",
+       NULL
+static void update_common_dir(struct strbuf *buf, int git_dir_len)
+       char *base = buf->buf + git_dir_len;
+       const char **p;
+       if (is_dir_file(base, "logs", "HEAD"))
+               return; /* keep this in $GIT_DIR */
+       for (p = common_list; *p; p++) {
+               const char *path = *p;
+               int is_dir = 0;
+               if (*path == '/') {
+                       path++;
+                       is_dir = 1;
+               }
+               if (is_dir && dir_prefix(base, path)) {
+                       replace_dir(buf, git_dir_len, get_git_common_dir());
+                       return;
+               }
+               if (!is_dir && !strcmp(base, path)) {
+                       replace_dir(buf, git_dir_len, get_git_common_dir());
+                       return;
+               }
+       }
 static void adjust_git_path(struct strbuf *buf, int git_dir_len)
        const char *base = buf->buf + git_dir_len;
@@ -101,6 +133,8 @@ static void adjust_git_path(struct strbuf *buf, int 
                              get_index_file(), strlen(get_index_file()));
        else if (git_db_env && dir_prefix(base, "objects"))
                replace_dir(buf, git_dir_len + 7, get_object_directory());
+       else if (git_common_dir_env)
+               update_common_dir(buf, git_dir_len);
 static void do_git_path(struct strbuf *buf, const char *fmt, va_list args)
diff --git a/t/ b/t/
index 1d29901..f9a77e4 100755
--- a/t/
+++ b/t/
@@ -241,5 +241,20 @@ test_expect_success 'setup fake objects directory foo' 
'mkdir foo'
 test_git_path GIT_OBJECT_DIRECTORY=foo objects foo
 test_git_path GIT_OBJECT_DIRECTORY=foo objects/foo foo/foo
 test_git_path GIT_OBJECT_DIRECTORY=foo objects2 .git/objects2
+test_expect_success 'setup common repository' 'git --git-dir=bar init'
+test_git_path GIT_COMMON_DIR=bar index                    .git/index
+test_git_path GIT_COMMON_DIR=bar HEAD                     .git/HEAD
+test_git_path GIT_COMMON_DIR=bar logs/HEAD                .git/logs/HEAD
+test_git_path GIT_COMMON_DIR=bar objects                  bar/objects
+test_git_path GIT_COMMON_DIR=bar objects/bar              bar/objects/bar
+test_git_path GIT_COMMON_DIR=bar info/exclude             bar/info/exclude
+test_git_path GIT_COMMON_DIR=bar remotes/bar              bar/remotes/bar
+test_git_path GIT_COMMON_DIR=bar branches/bar             bar/branches/bar
+test_git_path GIT_COMMON_DIR=bar logs/refs/heads/master   
+test_git_path GIT_COMMON_DIR=bar refs/heads/master        bar/refs/heads/master
+test_git_path GIT_COMMON_DIR=bar hooks/me                 bar/hooks/me
+test_git_path GIT_COMMON_DIR=bar config                   bar/config
+test_git_path GIT_COMMON_DIR=bar packed-refs              bar/packed-refs
+test_git_path GIT_COMMON_DIR=bar shallow                  bar/shallow

To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to
More majordomo info at

Reply via email to