示例:
$ cd lib
$ git absolute-path test.c # how to do this?
lib/test.c发布于 2013-06-06 07:00:58
至少从git 1.6.0开始,使用ls-files
$ cd lib
$ git ls-files --full-name test.c
lib/test.c对于较旧的git,请使用git ls-tree
$ cd lib
$ git ls-tree --full-name --name-only HEAD test.c
lib/test.c这只适用于已提交到repo中的文件,但总比什么都没有好。
发布于 2014-02-07 12:52:15
无论"test.c“当前是否存在,都可以将以下内容粘贴到bash终端中。为了方便起见,可以将git-absolute-path函数复制到.bashrc文件中。
git-absolute-path () {
fullpath=$([[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}")
gitroot="$(git rev-parse --show-toplevel)" || return 1
[[ "$fullpath" =~ "$gitroot" ]] && echo "${fullpath/$gitroot\//}"
}
git-absolute-path test.c发布于 2021-04-13 18:27:37
为了获得当前目录相对于git根目录的路径,我最终执行了以下操作:
if gitroot=$(git rev-parse --show-toplevel 2>/dev/null); then
directory=$(realpath --relative-to="$gitroot" .)
fi(我假设Bash和我不知道这有多便携。)
https://stackoverflow.com/questions/16951267
复制相似问题