Why some git commands are not allowed in the .git directory?
There seems to be some inconsistency as to which commands are allowed when you are in the .git
directory, and which are not. For instance
git symbolic-ref HEAD
or
git diff --staged
are fine.
But
git diff
or
git status
produces the error message: fatal: This operation must be run in a work tree
Even more surprising: create an alias of one of the failing commands above, like git st
for git status
, and then it works!
Is there any logical explanation for all that? And why would the alias of a failing command suddenly work just because it's an alias??
There is a logical explanation. The commands with arguments that failed did so because they require a work tree and when you are in a .git repository there is no work tree, only repository files. The other commands with arguments succeeded because they do not require a work tree.
There are a few Git commits (git wrapper: allow setup_git_directory_gently() be called earlier, Call setup_git_directory() much earlier and git --paginate: paginate external commands again) that touch on what's going on. The different behavior for builtin commands and aliases seems to stem from previous issues where builtin commands needed to get to Git configuration information earlier, which required determing the Git directory and working tree to get to repo-specific configurations.
If you have the Git source locally, you can execute git log --grep='setup_git_directory_gently'
to get a list of commits related to the behavior you're noticing.
For why aliases behave the way they do, Accept git aliases outside a git repository explains:
af05d67 (Always set *nongit_ok in setup_git_directory_gently(), 2008-03-25) had a change from the patch originally submitted that resulted in disabling aliases outside a git repository.
It turns out that some people used "alias.fubar = diff --color-words" in $HOME/.gitconfig to use non-index diff (or any command that do not need git repository) outside git repositories, and this change broke them, so this resurrects the support for such usage.
链接地址: http://www.djcxy.com/p/58654.html