VSCode + WSL Remote + Git : Synchronizing changes take forever

2021-02-05 UPDATE

This question got a lot of views recently and I felt like I needed to give a more thorough explanation. The original answer was written when WSL 2 was still in beta for testing purposes. Now that everything has evolved, removing the passphrase from an SSH-Key can lead to some vulnerabilities. So, before going any further with this, take a look at this post Is it okay to use a SSH key with an empty passphrase?

Ask yourself if it safe for you to remove it.

I don't know if there is still an issue with the orignal question. I gave up on WSL for web dev shortly after I wrote the question.

ORIGINAL ANWSER

I solved my issue. So, for thoses of you that want to give WSL 2 a try and encounter this, the issue is the passphrase of the SSH-key.

https://code.visualstudio.com/docs/remote/troubleshooting#_resolving-hangs-when-doing-a-git-push-or-sync-on-an-ssh-host

Resolving hangs when doing a Git push or sync on an SSH host
If you clone a Git repository using SSH and your SSH key has a passphrase, VS Code's pull and sync features may hang when running remotely.

Either use an SSH key without a passphrase, clone using HTTPS, or run git push from the command line to work around the issue.

If you want to remove your passphrase, use $ ssh-keygen -p as mentioned into this question: https://stackoverflow.com/a/112409/5543999


You can also get around this issue by using ssh-agent.

Add the following to your ~/.bashrc and whenever you open a terminal you will be prompted to unlock your key(s) if not unlocked.

env=~/.ssh/agent.env

agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }

agent_start () {
    (umask 077; ssh-agent >| "$env")
    . "$env" >| /dev/null ; }

agent_load_env

# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2= agent not running
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)

if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then
    agent_start
    ssh-add
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
    ssh-add
fi

unset env

Source: https://docs.github.com/en/github/authenticating-to-github/working-with-ssh-key-passphrases#auto-launching-ssh-agent-on-git-for-windows

Furthermore, you can configure the expiry of keys added to the ssh-agent as follows - https://unix.stackexchange.com/questions/122511/configuring-the-default-timeout-for-the-ssh-agent