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:

  1. Press Windows + R to open the Windows Run prompt.

  2. Type in sysdm.cpl and click OK.

  3. Open the Advanced tab and click on the Environment Variables button in the System Properties window.

  4. The Environment Variables window is divided into two sections: User variables and System variables. The following the changes will be made in User variables section.

    • Create these variables:

      Variable name Variable value
      HOME %USERPROFILE%
      MSYS2_PATH_TYPE inherit
    • Add the following directories to the PATH variable:

      • C:\msys64
      • C:\msys64\usr\bin
      • C:\msys64\mingw64\bin

NOTE: The MSYS2_PATH_TYPE and PATH variables can be set as System variables, making them effective for all users. The HOME environment variable, however, must be set for each user in the User variables section.

Install MSYS2

  1. Download and run the installer from https://www.msys2.org/
  2. Accept the defaults for every step of the installer.
  3. 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 in 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 in the MinGW console

NOTE: The Git CLI does not work under the msys2.exe console! Close the msys2.exe window and launch C:\msys64\mingw64.exe for the remaining steps, as well as day-to-day use of Bash and Git.

Install Git for Windows

Run the following command:

$ pacman -Sy git-for-windows-x86_64/mingw-w64-x86_64-{git,git-doc-html,git-doc-man,git-extra,git-credential-manager,curl-winssl}

Configure the documentation path

Run the following command:

$ git config --system help.htmlPath /mingw64/share/doc/git-doc

Results

Instead of just having Git installed, you now have a full-blown POSIX environment on Windows and a Linux-style shell and package manager. If you are coming from a Linux or MacOS environment, you will find this interface much more comfortable. Just like Linux or Homebrew, you can install any package you need using pacman. Here are some other features: