git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [idea] separate .git dir and the working tree

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Feb 5, 2011, 18:04 UTC
Message-ID
<20110205180425.GA18961@elie>
In-Reply-To
<AANLkTinYMuXSvKjgtpSCbJPuo=PHDGR_OgOkwtV4Azmz@mail.gmail.com>
redstun wrote:
Show 9 quoted lines
> what I wanted is, I may have multiple git working tree in my $HOME, like:
> $HOME/proj1
> $HOME/proj2,
> 
> I then wanted their .git directories respectively located at
> /safe/disk/.git_proj1
> /safe/disk/.git_proj2
> 
> How can I get this? I tried to read the doc but didn't have much luck.

By making .git in $HOME/proj1 a symlink to /safe/disk/.git_proj1 and making $HOME/proj2/.git a symlink to /safe/disk/.git_proj2. GIT_DIR should be left unset for this to work.

The GIT_DIR variable is for more complicated use cases in which you do not want a .git directory, file, or symlink in the worktree at all.

One uses it as follows:
	GIT_DIR=/somewhere/else/.git git <command> ...

The top level of the work tree is inferred to be the current working directory and git metadata is taken from somewhere else.

Sometimes a person does not only want to work in the top level of the working tree. To tell git where the top level is, use the GIT_WORK_TREE variable:

	(
		GIT_DIR=/somewhere/else/.git; export GIT_DIR
		GIT_WORK_TREE=$(pwd); export GIT_WORK_TREE
		cd sub/directory
		git <command>...
		cd ../other/directory
		git <other command>...
	)

These are not variables for your .profile, since as you mentioned, a person typically will work with one git repo and worktree sometimes, another git repo and work tree another time.

Hope that helps, Jonathan

Side note: I don't think you mentioned wanting it, but for reference, there is a nice tool for sharing objects between multiple worktrees in contrib/worktree. It comes with some sharp edges, as described in [1].

[1] http://thread.gmane.org/gmane.comp.version-control.git/150559
Previous: redstunNext: Mike Gant
Message 5 of 12 in “[idea] separate .git dir and the working tree”
  1. redstunFeb 5, 2011
  2. Mike GantFeb 5, 2011
  3. Jonathan NiederFeb 5, 2011
  4. redstunFeb 5, 2011
  5. Jonathan NiederFeb 5, 2011
  6. Mike GantFeb 6, 2011
  7. Mike GantFeb 6, 2011
  8. Jared HanceFeb 6, 2011
  9. Konstantin KhomoutovFeb 6, 2011
  10. [idea] Re: separate .git dir and the working treeNicolas Sebrecht, Feb 6, 2011
  11. Mike GantFeb 7, 2011
  12. Michael WittenMay 23, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.