Subject: Re: [DOC] more explanation about --git-dir and --work-tree options

From: Michael J Gruber <[email-masked]>
Date: Tue, 29 Apr 2008 17:49:44 +0200

Liu Yubao venit, vidit, dixit 28.04.2008 22:09:
> Hi,
> 
> I find these two options bring me surprise:
> 
>    git init $HOME
>    git add ~/.vimrc ~/.gvimrc ~/.vim
>    cd $HOME/work/xxx
>    ....do some work, then change ~/.vimrc without changing
>        working directory
>    git --git-dir $HOME/.git status
> 
> I use --git-dir because I have another .git in $HOME/work/xxx, the
> last command surprises me much, it tells me .vim* are all deleted!
> 
> After checking the code, I realize git thinks the current working
> directory as top directory of the working tree if --git-dir is specified
> without --work-tree option.

Yes, I found this surprising at first, too. And I noticed later on why 
things are the way they are, just as you did. :)

More doc on this is certainly helpful.

> And here is my supplement to the documentation.

> +	variable and the '--work-tree' command line option. It can be
> +	a absolute path or relative path to the directory specified by

"an absolute path or a relative...""

> +	--git-dir or GIT_DIR.
> +	Note: If --git-dir or GIT_DIR are specified but none of
> +	--work-tree, GIT_WORK_TREE and core.worktree is specified,

I would probably interchange "is" and "are" here.

Same changes in the following.

I often find myself wanting to operate on a repo without cd'ing to it. While

( cd repo && git command args)

is certainly an option I find the following shell functions helpful:

alias g=git
function gg ()
{
   local _gg="$1";
   shift;
   git --git-dir="${_gg}/.git" --work-tree="${_gg}" "$@"
}

That way,

gg repo command args

runs git in a different location while

g command args

works as usual (and is shorter).

Now I'm just waiting for the experts to tell me that cd'ing to repo is 
completely different from specifying --git-dir and --work-tree, and that 
either way is completely wrong. (I'm still stupid and ugly, but I want 
to become clever and smart.) ;)

Michael