7

How I can get current working dir from console using git?

something like: if I'm in: ~/projects/someproject/somesubfolder/, the console command should say /home/va1en0k/someproject/ (if there is a .git/ folder in this directory or something like this)

or if GIT_WORKING_DIR is set, it should return it. or whatever Git uses to determine it

Sathyajith Bhat
  • 60,620
  • 37
  • 177
  • 263
valya
  • 838
  • 3
  • 14
  • 29

2 Answers2

15

Simple: git rev-parse --show-toplevel

sorin
  • 11,071
  • 20
  • 59
  • 72
10

Try this:

echo $( cd $(git rev-parse --show-cdup); pwd)
  • 11
    I looked at git-rev-parse(1). It seems like "git rev-parse --show-toplevel" will suffice. Thank you! :-) – valya Apr 10 '11 at 19:03
  • 1
    Is the outer command substitution really needed here? Running in a subshell (`(cd … ; pwd)`) may be enough. Please see [What is wrong with `echo $(stuff)`?](https://superuser.com/q/1352850/432690) – Kamil Maciorowski Mar 09 '20 at 17:14
  • The problem is, that `git rev-parse --show-toplevel` and your comment both dont work when you are in the `.git` dir e.g. in a hook-script – Radon8472 Sep 02 '20 at 07:30