A Better Git on Windows
Introduction
The standard Git installation comes with a Bash shell and a basic POSIX environment, but it’s not easy to use or customize. The instructions below show how to set up a full-featured POSIX environment that will rival native Linux or macOS installations for usability.
Maybe you are accustomed to a UNIX-style shell, but are forced to use Windows. Or maybe you like Windows but want a more functional shell. This is the setup you’ve been looking for!
Prerequisites
Set up user environment variables
To modify environment variables, follow these steps:
- Press Windows + R to open the Windows Run prompt.
- Type in
sysdm.cpland click OK. - Open the Advanced tab and click on the Environment Variables button in the System Properties window.
- The Environment Variables window is divided into two sections. We will use the top section for user-specific environment variables. Create the following environment variables for the user:
| Variable name | Variable value |
|---|---|
HOME |
C:\Users\<username> |
MSYS2_PATH_TYPE |
inherit |
- Add the following MSYS2 directories to the user’s
PATHvariable:
C:\msys64C:\msys64\usr\binC:\msys64\mingw64\bin
Install MSYS2
- Download and run the installer from https://www.msys2.org/
- Accept the defaults for every step of the installer.
- Once the installation is complete, uncheck the option to Run MSYS2 now and click Finish.
Edit nsswitch.conf
Update the db_home line in C:\msys64\etc\nsswitch.conf to look like this:
db_home: windows cygwin desc
Update pacman configuration
Edit C:\msys64\etc\pacman.conf and add the following repository definition.
It needs to be the first repository, so that packages from it are installed first.
[git-for-windows]
Server = https://wingit.blob.core.windows.net/x86-64
Steps for the MSYS2 console
For the following instructions, launch C:\msys64\msys2.exe and run commands in the resulting console window.
Authorize a signing key
Run the following commands:
$ curl -L https://raw.githubusercontent.com/git-for-windows/build-extra/master/git-for-windows-keyring/git-for-windows.gpg | pacman-key --add -
$ pacman-key --lsign-key E8325679DFFF09668AD8D7B67115A57376871B1C
Install packages
Run the following command to install prerequisite packages
$ pacman -Sy man vim nano openssh rsync make zip unzip mingw64/mingw-w64-x86_64-jq
Update the MSYS2 runtime
Run the following command:
$ pacman -Syu
This will guide you through installing a newer MSYS2 runtime. If there is an update available, the process will usually cause your console to terminate. This is expected.
Update packages
Relaunch C:\msys2.exe and run the update command again.
$ pacman -Syu
Steps for the mingw console
NOTE: The Git CLI does not work under the
msys2.execonsole! Close themsys2.exewindow and launchC:\msys64\mingw64.exefor the remaining steps, as well as day-to-day use of Bash and Git.
Install Git for Windows
Run the following command:
$ pacman -Sy mingw-w64-x86_64-{git,git-doc-html,git-doc-man,curl}
Configure the documentation path
Run the following command:
$ git config --system help.htmlPath /mingw64/share/doc/git-doc
Results
It may not be obvious at this point how this is any better than just running the standard Git for Windows installation. I’ll provide more articles on how to take advantage of the environment we’ve installed. Here are some of the features we can look forward to:
- Enable an SSH agent so you don’t have to type your passphrase after every Git push/pull
- Install your favorite shell: zsh or fish
- Use a UNIX shell for all your CLI interactions on Windows