Jo *_*iss 24

至少git 1.6.0,使用 ls-files:

$ cd lib
$ git ls-files --full-name test.c
lib/test.c
Run Code Online (Sandbox Code Playgroud)

使用 git ls-tree:

$ cd lib
$ git ls-tree --full-name --name-only HEAD test.c
lib/test.c
Run Code Online (Sandbox Code Playgroud)

这仅适用于已提交到repo的文件,但它总比没有好.


gma*_*tht 5

无论"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
Run Code Online (Sandbox Code Playgroud)