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

前端 未结 22 974
被撕碎了的回忆
被撕碎了的回忆 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: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.

提交回复
热议问题