Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Portable Git in Node.js command prompt on Windows

GitHub for Windows doesn't put Git in the PATH by default. If you'd like your Node.js command prompt to have the git command available by default, simply edit your nodevars.bat file. By default, this is in C:\Program Files\nodejs\. You will have to run your text editor in an administrative context for this to work.

Replace this line in your nodevars.bat file:

set PATH=%APPDATA%\npm;%~dp0;%PATH%

With these thre lines:

for /F %%A in ('"dir /s /b /OD %userprofile%\appdata\local\github\portableGit_*"') do set gitPath=%%A\bin
IF NOT EXIST "%gitPath%\git.exe" SET gitPath=%gitPath%\..\cmd
set PATH=%APPDATA%\npm;%~dp0;%PATH%;%gitPath%

That will set a variable called %gitPath% with the location of git.exe, and then append it at the end of your path in the Node.js command prompt. Because of the /OD switch, it will use the version of Portable Git whose folder has the latest modified date. If Portable Git is not found, you will get a harmless extra ; in the %path%.

for /F %%A in ('"dir /s /b /OD %userprofile%\appdata\local\github\portableGit_*"') do set gitPath=%%A\bin
IF NOT EXIST "%gitPath%\git.exe" SET gitPath=%gitPath%\..\cmd
set PATH=%APPDATA%\npm;%~dp0;%PATH%;%gitPath%
set PATH=%APPDATA%\npm;%~dp0;%PATH%
Owner

nycdotnet commented Mar 6, 2016

Updated March 2016 due to change in GitHub for Windows to store git.exe under cmd subfolder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment