9

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

2 Answers 2

11

Try this:

echo $( cd $(git rev-parse --show-cdup); pwd)
3
  • 12
    I looked at git-rev-parse(1). It seems like "git rev-parse --show-toplevel" will suffice. Thank you! :-)
    – valya
    Commented Apr 10, 2011 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)? Commented Mar 9, 2020 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
    Commented Sep 2, 2020 at 7:30
21

Simple: git rev-parse --show-toplevel

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .