Is there a way to get the git root directory in one command?

前端 未结 22 1223
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 09:57

Mercurial has a way of printing the root directory (that contains .hg) via

hg root

Is there something equivalent in git to get the director

22条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 10:51

    I wanted to expand upon Daniel Brockman's excellent comment.

    Defining git config --global alias.exec '!exec ' allows you to do things like git exec make because, as man git-config states:

    If the alias expansion is prefixed with an exclamation point, it will be treated as a shell command. [...] Note that shell commands will be executed from the top-level directory of a repository, which may not necessarily be the current directory.

    It's also handy to know that $GIT_PREFIX will be the path to the current directory relative to the top-level directory of a repository. But, knowing it is only half the battle™. Shell variable expansion makes it rather hard to use. So I suggest using bash -c like so:

    git exec bash -c 'ls -l $GIT_PREFIX'
    

    other commands include:

    git exec pwd
    git exec make
    

提交回复
热议问题