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

Re: [PATCH] Teach git to change to a given directory using -C option

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Apr 20, 2013, 22:18 UTC
Message-ID
<20130420221752.GA9980@elie.Belkin>
In-Reply-To
<20130419161250.GC14263@sigill.intra.peff.net>
Jeff King wrote:
> On Fri, Apr 19, 2013 at 08:21:48PM +0800, Nazri Ramliy wrote:
Show 10 quoted lines
>> Often I find myself needing to find out quickly the status of a repository that
>> is not in my currenct working directory, like this:
>>
>>          $ (cd ~/foo; git log -1)
>>
>> With this patch now i can simply do:
>>
>>          $ git -C ~/.zsh log -1 
>> 
>> That's just one example.
[...]
> You can _almost_ do this with "git --git-dir". But it expects the actual
> git directory, not a starting point for finding the git directory.
[...]
> It is redundant with "(cd foo && git ...)" in the shell, as you note,
> but sometimes it is more convenient to use "-C" (especially if you are
> exec-ing git from another program and want to avoid the shell entirely
> for quoting reasons).

When I want to run "git log" for a repository outside the cwd, I do use --git-dir (or more precisely,

	$ GIT_DIR=$HOME/src/git/.git git log
), which works.

The "sometimes you just want to pass a command to 'exec'" use case does not convince me. I equally well might want to run "git" after another command, or run "git" if and only if a repository exists there, or do any number of other things. If someone asked me how to do that by passing a command to 'exec', I'd point them to

	sh -c 'cd foo && git ...'

as a way to answer all such questions at the same time. So we're left with "--git-dir does not automatically append .git when appropriate" as the problem being solved, which is a real problem. Maybe that is worth fixing more directly?

It might also be convenient to be able to do something like
	git --git-dir=~/src/git log -- Documentation/

which this -C option makes easy. *checks* Actually it works without, but for subtle reasons. A more sensible way to spell that is

	git --git-dir=<wherever> -- :/Documentation/
which works fine.

All that said, I don't mind -C terribly as long as it can maintain itself, which means including thorough documentation that covers the purpose and how pathname parameters and envvars interact with the new option and including tests under t/ to ensure it continues to work correctly in the future.

Thanks for an interesting patch, and hope that helps, Jonathan

Previous: Jeff KingNext: Jeff King
Message 3 of 5 in “Teach git to change to a given directory using -C option”
  1. Teach git to change to a given directory using -C optionNazri Ramliy, Apr 19, 2013
  2. Jeff KingApr 19, 2013
  3. Jonathan NiederApr 20, 2013
  4. Jeff KingApr 21, 2013
  5. Teach git to change to a given directory using -C optionNazri Ramliy, Apr 21, 2013

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.