Skip to content

Instantly share code, notes, and snippets.

@nycdotnet
Last active April 28, 2018 14:57
Show Gist options
  • Save nycdotnet/f7d7b8de0c55b7081cb0 to your computer and use it in GitHub Desktop.
Save nycdotnet/f7d7b8de0c55b7081cb0 to your computer and use it in GitHub Desktop.
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\git.exe"') do set gitPath=%%A\..
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\git.exe"') do set gitPath=%%A\..
IF NOT EXIST "%gitPath%\git.exe" SET gitPath=%gitPath%\..\cmd
set PATH=%APPDATA%\npm;%~dp0;%PATH%;%gitPath%
set PATH=%APPDATA%\npm;%~dp0;%PATH%
@nycdotnet
Copy link
Author

Updated April 2018 due to GitHub Desktop putting git in different spot again.

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