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


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
  • 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)? – Kamil Maciorowski Mar 9 '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 2 '20 at 7:30

Simple: git rev-parse --show-toplevel

  • 1
    How is this different from 'valya Apr 10 '11 at 19:03' comment below the accepted answer, we can all miss things. – mic84 Aug 23 '18 at 10:39
  • 2
    A comment is not an answer, it cannot be accepted. – sorin Aug 26 '18 at 7:47
  • --show-toplevel will be an absolute path apparently, and --show-cdup will be relative to cwd. – Alexander Bird Oct 29 '19 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.