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: 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_TYPEinherit -
Add the following directories to the
PATHvariable:C:\msys64C:\msys64\usr\binC:\msys64\mingw64\bin
-
NOTE: The
MSYS2_PATH_TYPEandPATHvariables can be set as System variables, making them effective for all users. TheHOMEenvironment variable, however, must be set for each user in the User variables section.
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 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.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 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:
- 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