8

Git for Windows using WSL

 2 years ago
source link: https://82phil.github.io/wsl/2019/12/12/wsl_cross_plat_git.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Git for Windows using WSL

Dec 12, 2019

For several years I used Git for Windows to manage repositories and it was decent enough. However, I found the Unix-like environment provided by Git for Windows to be very different than what I was used to. I have longed for a way to use Git from WSL to manage my repositories and continue to support tooling dependent on Git that ran on Windows

What’s the catch?

This assumes that you use SSH keys for authentication. If you use passphrases on your keys then an ssh-agent should be setup. If you use HTTPS authentication then it will require a workaround to function in WSL.

Performance

In my day to day activities running Git from WSL, I have not found performance to be different between the two. Below is a quick comparison of cloning a repository.

git clone using Git for Windows git clone - Git for Windows

git clone using WSL git clone - WSL

Note: WSL had improvements from WSL Disk performance

Moving the Git configuration to WSL

Git for Windows by default uses your user profile (%USERPROFILE%) directory to store configuration and SSH keys. Since the systems are mostly the same, you can copy them over to WSL.

cd /mnt/c/Users/<your_username>
mv .gitconfig ~
mv .ssh/ ~

The private SSH key file permissions need to be updated, follow the step below for your keys.

chmod 600 ~/.ssh/<your_key>

Line Endings

Git may convert line endings to CRLF on checkout. If you are not paying attention this can cause some head-scratching during code reviews. Setting autocrlf to input will checkout with line endings left as-is, and will convert them to LF when committing.

Enter the following in your WSL console.

git config --global core.autocrlf input

It is recommended that repositories contain .gitattrbutes; a configuration file that defines the desired line endings that will override the user settings. See Configuring Git to Handle Line Endings for further information on configuration Git Attributes.

Use WSL Git from Windows

Using the WSL command line, you can clone, commit, push, etc. without any issues. However, tools from the Windows side, such as VS Code, will still be calling to Git for Windows for Git operations. Luckily there is a tool called wslgit to forward those calls to WSL git.

  1. Create a directory under %programfiles% called wslgit.
  2. Download the wslgit tool and save it under the folder created earlier.
  3. Rename wslgit.exe to git.exe
  4. Update the PATH user environment variable to include the path git.exe under %programfiles%\wslgit. If Git for Windows is also in the path make sure this precedes it.
  5. Create a new user environment variable WSLGIT_USE_INTERACTIVE_SHELL and set the value to 0.

At this point, you should be able to run a PowerShell or Windows CMD session and execute git. You may have to restart an existing session for changes to take effect.

Finally, I recommend uninstalling Git for Windows. Switching between Git for Windows and using Git in WSL in the same repository will likely break it.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK