eedrah
eedrah

Reputation: 2385

How can I execute a git alias from a specific directory, not the root of the repo

Git seems to execute all the aliases from the root of the repo. You can see this by using the following alias:

test = !pwd

or even

test2 = "!f() { pwd ;}; f"

which will both display the directory of the root of the repo, not the subdirectory that you are in.

How can I make an alias that executes from the directory that you are in, not the root?

Upvotes: 2

Views: 201

Answers (1)

eedrah
eedrah

Reputation: 2385

You can use the variable $GIT_PREFIX. For example:

test = !echo $GIT_PREFIX

or a more valid use-case:

cat-from = "!f() { git show $1:\"$GIT_PREFIX$2\" ;}; f"

which can be used to cat a file from a particular branch without checking it out: git cat-from master foo.txt

I found this by looking in the source code here: https://github.com/git/git/blob/35f6318d44379452d8d33e880d8df0267b4a0cd0/t/t1020-subdirectory.sh#L131

Please add an edit/comment/answer if you can find where this is documented.

Upvotes: 1

Related Questions