8

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
  • 61,504
  • 38
  • 179
  • 264
valya
  • 888
  • 3
  • 14
  • 29

2 Answers2

19

Simple: git rev-parse --show-toplevel

sorin
  • 11,660
  • 20
  • 63
  • 73
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
  • 1
    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