andy-5 / wslgit

Use Git installed in Bash on Windows/Windows Subsystem for Linux (WSL) from Windows and Visual Studio Code (VSCode)
MIT License
1.18k stars 59 forks source link

change default work directory in ~/.bashrc will make fork's fetch/push failed #114

Closed toukoubun closed 3 years ago

toukoubun commented 3 years ago

The default work directory when I start wsl2 is /mnt/c/Users/user_name To make the default work directory becomes my home, I added the command cd ~ to ~/.bashrc

After doing this, when I try to fetch/push in fork, the error happened

fatal: not a git repository (or any of the parent directories): .git
andy-5 commented 3 years ago

For git to work, the work directory needs to be inside the git repository. When your .bashrc is executed, the work directory will be changed, and the git repository is not found.

One way to fix this is to change the work directory only when bash is not invoked with the -c flag (to execute a command), i.e. replace your cd ~ with something like this (untested):

[[ $- != *c* ]] && cd ~

Please report back if this fixes the problem for you.

toukoubun commented 3 years ago

Thank you! After changing cd ~ to [[ $- != *c* ]] && cd ~, this problem is fixed!