2012-08-15 85 views
3

可能重複:
Is there a way to get the git root directory in one command?獲取git工作目錄根的腳本?

我寫一個bash腳本(實際上是一個Makefile文件)需要使用一個參數,相對於Git的工作目錄的根目錄包含當前目錄。

即:

/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如下:

echo hello `git something`/baz 

是什麼混帳東西嗎?它應該返回當前git工作根目錄的絕對路徑。

回答

9

好吧,如果你知道你要在酒吧的文件夾,你可以嘗試:

echo hello `pwd`/../baz 

編輯:@Andrew Tomazos前面所指出的,以不同的線程,你可以做

echo hello `git rev-parse --show-toplevel`/baz 
+1

我覺得這應該是正確的答案 – xis 2015-04-29 20:39:38