ewok
ewok

Reputation: 21443

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

I've seen this question, which tells how to get the path of a particular file relative to the root of the git repo. But I now want to get the path of the current directory, not a specific file. If I use

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

I get the list of all the files in the directory.

Is this possible?

Upvotes: 27

Views: 28384

Answers (3)

Peter W
Peter W

Reputation: 1080

Adding onto the great answer from Arkadiusz Drabczyk, I just wrapped the suggested command in a shell script (calling it rd.sh, standing for "relative directory" or "repository directory"), and make it print "/" if the git rev-parse --show-prefix command would return nothing (at the top level). I'm just starting to use this now, so we'll see how it works!

#!/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

Upvotes: 0

strobelight
strobelight

Reputation: 267

#git ls-files --full-name  | xargs -L1 dirname | sort -u

Use that git rev-parse --show-prefix above, awesome

Upvotes: 0

Arkadiusz Drabczyk
Arkadiusz Drabczyk

Reputation: 12363

How about:

$ git rev-parse --show-prefix

From 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.

Upvotes: 48

Related Questions