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
  • 62,609
  • 36
  • 171
  • 294

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
  • 3,876
  • 7
  • 31
  • 35
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
  • 1,864
  • 1
  • 15
  • 17
  • 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