git alias for shell command to cd into git root not working as expected git alias for shell command to cd into git root not working as expected shell shell

git alias for shell command to cd into git root not working as expected


Your shell is invoking Git, and Git is invoking another shell in which to run your cd command. This command is successful, and this changes the working directory of the child shell, but it does not change the working directory of Git, nor of the parent shell.

In order to do this you need to run the command in your current shell, which means that invoking Git will not be able to accomplish this. You will have to continue using a shell alias.


To illustrate, let's say you have the following shell script called up.sh:

#!/bin/shcd ..

If you execute this script as ./up.sh then nothing will change from the perspective of your current shell, because cd was executed in a new shell instance. However, if you execute it as . up.sh, this instructs your current shell to execute the contents of the file by itself, without spawning a subshell. In that case the current shell's working directory will change.

That's the key difference here. Using a Git alias is similar to the ./up.sh method, while a shell alias is similar to the . up.sh method.