繁体   English   中英

如何获取当前目录相对于git仓库根目录的路径?

[英]How to get the path of the current directory relative to root of the git repository?

我见过这个问题,它告诉如何获取特定文件相对于 git repo 根目录的路径。 但是我现在想获取当前目录的路径,而不是特定文件。 如果我使用

git ls-tree --full-name --name-only HEAD .

我得到目录中所有文件的列表。

这可能吗?

怎么样:

$ git rev-parse --show-prefix

来自man git-rev-parse

--show-prefix
           When the command is invoked from a subdirectory, show
           the path of the current directory relative to the top-level
           directory.

添加到 Arkadiusz Drabczyk 的出色答案中,我只是将建议的命令包装在一个 shell 脚本中(称为 rd.sh,代表“相对目录”或“存储库目录”),并在git rev-parse --show-prefix打印“/” git rev-parse --show-prefix命令将不返回任何内容(在顶层)。 我现在才开始使用它,所以我们将看看它是如何工作的!

#!/bin/bash
# rd.sh: shows the "repository directory" rather than the entire absolute dir from 'pwd'

RELATIVE_DIR=`git rev-parse --show-prefix`

if [ -z "$RELATIVE_DIR" ]; then
  echo "/"
else
  echo $RELATIVE_DIR
fi
#git ls-files --full-name  | xargs -L1 dirname | sort -u

使用上面的git rev-parse --show-prefix ,真棒

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM