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

前端 未结 22 1025
被撕碎了的回忆
被撕碎了的回忆 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:55
    $ git config alias.root '!pwd'
    # then you have:
    $ git root
    
    0 讨论(0)
  • 2020-11-22 10:58

    Here is a script that I've written that handles both cases: 1) repository with a workspace, 2) bare repository.

    https://gist.github.com/jdsumsion/6282953

    git-root (executable file in your path):

    #!/bin/bash
    GIT_DIR=`git rev-parse --git-dir` &&
    (
      if [ `basename $GIT_DIR` = ".git" ]; then
        # handle normal git repos (with a .git dir)
        cd $GIT_DIR/..
      else
        # handle bare git repos (the repo IS a xxx.git dir)
        cd $GIT_DIR
      fi
      pwd
    )
    

    Hopefully this is helpful.

    0 讨论(0)
  • 2020-11-22 10:58

    Since Git 2.13.0, it supports a new option to show the path of the root project, which works even when being used from inside a submodule:

    git rev-parse --show-superproject-working-tree
    
    0 讨论(0)
  • 2020-11-22 11:01

    Pre-Configured Shell Aliases in Shell Frameworks

    If you use a shell framework, there might already be a shell alias available:

    • $ grt in oh-my-zsh (68k) (cd $(git rev-parse --show-toplevel || echo "."))
    • $ git-root in prezto (8.8k) (displays the path to the working tree root)
    • $ g.. zimfw (1k) (changes the current directory to the top level of the working tree.)
    0 讨论(0)
提交回复
热议问题