Get started using Git on Windows Subsystem for Linux
Git is the most commonly used version control system. With Git, you can track changes you make to files, so you have a record of what has been done, and have the ability to revert to earlier versions of the files if needed. Git also makes collaboration easier, allowing changes by multiple people to all be merged into one source.
Git can be installed on Windows AND on WSL
An important consideration: when you enable WSL and install a Linux distribution, you are installing a new file system, separated from the Windows NTFS C:\ drive on your machine. In Linux, drives are not given letters. They are given mount points. The root of your file system
/ is the mount point of your root partition, or folder, in the case of WSL. Not everything under
/ is the same drive. For example, on my laptop, I've installed two version of Ubuntu (20.04 and 18.04), as well as Debian. If I open those distributions, select the root directory with the command
cd ~, and then enter the command
explorer.exe ., Windows File Explorer will open and show me the directory path for that distribution.
|Linux distro||Windows Path to access home folder|
If you are seeking to access the Windows file directory from your WSL distribution command line, instead of
C:\Users\username, the directory would be accessed using
/mnt/c/Users/username, because the Linux distribution views your Windows file system as a mounted drive.
You will need to install Git on each file system that you intend to use it with.
Git comes already installed with most of the Windows Subsystem for Linux distributions, however, you may want to update to the latest version. You also will need to set up your git config file.
To install Git, see the Git Download for Linux site. Each Linux distribution has their own package manager and install command.
For the latest stable Git version in Ubuntu/Debian, enter the command:
sudo apt-get install git
You also may want to install Git for Windows if you haven't already.
Git config file setup
To set up your Git config file, open a command line for the distribution you're working in and set your name with this command (replacing "Your Name" with your Git username):
git config --global user.name "Your Name"
Set your email with this command (replacing "firstname.lastname@example.org" with the email you use on your Git account):
git config --global user.email "email@example.com"
If you don't yet have a Git account, you can sign-up for one on GitHub. If you've never worked with Git before, GitHub Guides can help you get started. If you need to edit your git config, you can do so with a built-in text editor like nano:
We recommend that you secure your account with two-factor authentication (2FA).
Git Credential Manager setup
Git Credential Manager enables you to authenticate a remote Git server, even if you have a complex authentication pattern like two-factor authentication, Azure Active Directory, or using SSH remote URLs that require an SSH key password for every git push. Git Credential Manager integrates into the authentication flow for services like GitHub and, once you're authenticated to your hosting provider, requests a new authentication token. It then stores the token securely in the Windows Credential Manager. After the first time, you can use git to talk to your hosting provider without needing to re-authenticate. It will just access the token in the Windows Credential Manager.
To set up Git Credential Manager for use with a WSL distribution, open your distribution and enter this command:
git config --global credential.helper "/mnt/c/Program\ Files/Git/mingw64/libexec/git-core/git-credential-manager.exe"
Now any git operation you perform within your WSL distribution will use the credential manager. If you already have credentials cached for a host, it will access them from the credential manager. If not, you'll receive a dialog response requesting your credentials, even if you're in a Linux console.
If you are using a GPG key for code signing security, you may need to associate your GPG key with your GitHub email.
Adding a Git Ignore file
We recommend adding a .gitignore file to your projects. GitHub offers a collection of useful .gitignore templates with recommended .gitignore file setups organized according to your use-case. For example, here is GitHub's default gitignore template for a Node.js project.
If you choose to create a new repo using the GitHub website, there are check boxes available to initialize your repo with a README file, .gitignore file set up for your specific project type, and options to add a license if you need one.
Git and VS Code
Visual Studio Code comes with built-in support for Git, including a source control tab that will show your changes and handle a variety of git commands for you. Learn more about VS Code's Git support.
Git line endings
If you are working with the same repository folder between Windows, WSL, or a container, be sure to set up consistent line endings.
Since Windows and Linux use different default line endings, Git may report a large number of modified files that have no differences aside from their line endings. To prevent this from happening, you can disable line ending conversion using a
.gitattributes file or globally on the Windows side. See this VS Code doc about resolving Git line ending issues.