4

Possible Duplicate:
Is there a way to get the git root directory in one command?

I am writing a bash script (actually a Makefile) that needs to use an argument that is a directory relative to the root of the git working directory that contains the current directory.

That is:

 /home/me$ git clone /abc/foo.git

 directory foo created

 /home/me$ cd foo/bar

 /home/me/foo/bar$ execute_script.sh

 hello /home/me/foo/baz

execute_script.sh is as follows:

 echo hello `git something`/baz

What is git something ? It should return the absolute path of the current git working root.

Community
  • 1
  • 1
Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319

2 Answers2

12

Well, if you know you're going to be in the bar folder, you can try:

echo hello `pwd`/../baz

Edit: As @Andrew Tomazos pointed out earlier to a different thread, you can do

echo hello `git rev-parse --show-toplevel`/baz
Tech163
  • 4,176
  • 8
  • 33
  • 36
1

This is what you are looking for

Is there a way to get the git root directory in one command?

root="'git rev-parse --git-dir`/.."

echo hello $root/baz

Community
  • 1
  • 1
Raghu
  • 2,004
  • 1
  • 16
  • 18
  • 1
    This answer actually doesn't work if you're in a git worktree. Using `git rev-parse --show-toplevel` seems to work in all cases that I tried. – staktrace Dec 21 '18 at 03:48