【问题标题】:Script getting git working directory root? [duplicate]获取git工作目录根的脚本? [复制]
【发布时间】:2012-08-11 17:43:06
【问题描述】:

可能重复:
Is there a way to get the git root directory in one command?

我正在编写一个 bash 脚本(实际上是一个 Makefile),它需要使用一个参数,该参数是一个相对于包含当前目录的 git 工作目录的根目录。

即:

 /home/me$ git clone /abc/foo.git

 directory foo created

 /home/me$ cd foo/bar

 /home/me/foo/bar$ execute_script.sh

 hello /home/me/foo/baz

execute_script.sh如下:

 echo hello `git something`/baz

什么是 git 东西?它应该返回当前 git 工作根目录的绝对路径。

【问题讨论】:

    标签: linux git bash


    【解决方案1】:

    这就是你要找的东西

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

    root="'git rev-parse --git-dir`/.."

    回声你好 $root/baz

    【讨论】:

    • 如果您在 git 工作树中,此答案实际上不起作用。在我尝试过的所有情况下,使用git rev-parse --show-toplevel 似乎都有效。
    【解决方案2】:

    好吧,如果你知道你会在 bar 文件夹中,你可以试试:

    echo hello `pwd`/../baz
    

    编辑:正如@Andrew Tomazos 之前指出的另一个线程,您可以这样做

    echo hello `git rev-parse --show-toplevel`/baz
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-09
      • 2016-04-30
      • 2018-08-18
      • 1970-01-01
      • 2013-07-01
      • 2011-11-03
      • 1970-01-01
      • 2014-09-28
      相关资源
      最近更新 更多