I'm trying to define an alias to cd
me into the git root directory using git rev-parse --show-toplevel
.
Currently I have it as an alias in my .bashrc
, which works fine:
alias gitroot='cd "$(git rev-parse --show-toplevel)"'
But I know that you can run shell commands using git aliases, so I thought I'd move the command into my .gitconfig
to make my files a bit cleaner.
Problem is, I can't figure out why my alias isn't working. Currently I have this under [alias]
in my .gitconfig
:
root = "!sh -c 'cd \"$(git rev-parse --show-toplevel)\"'"
But when I run git root
, I just stay put in the current directory.
I've experimented with other shell aliases and they've worked, e.g. the alias say = "!sh -c 'echo \"$0\"'"
works just fine:
$ git say hello
hello
Though I've noticed that a similar cd
command (cd = "!sh -c 'cd \"$0\"'"
) fails:
~/repos/myproject$ git cd /home/
~/repos/myproject$
I can't figure out if it's something to do with my syntax or if it's a quirk of git aliases that it doesn't support the cd
command, or something else entirely.
If it helps, I'm using Git 1.7.9.5.