How to Add Git to PATH on Windows
-
Understanding the Crucial Role of
PATH
-
Manually Add
git
to thePATH
on Windows -
Add
git
to thePATH
on Windows using Batch Script -
Add git to
PATH
using PowerShell - Conclusion
Git is a free, open-source version control system designed to handle projects quickly and efficiently. You can use this on Windows, Mac and Linux operating systems. Whether you’re collaborating with a team or working on personal projects, Git empowers you to track changes, collaborate effectively, and manage your codebase efficiently.
This article will explain how to add the git to the Windows PATH environment variable.
Understanding the Crucial Role of PATH
Before we dive into the step-by-step guide, it’s worth understanding why adding Git to the PATH
environment variable is crucial.
The PATH
variable is a list of directories that the operating system searches for executable files when you run a command.
By adding Git’s directories to the PATH, you enable seamless access to Git’s commands from any directory in the command prompt.
Manually Add git
to the PATH
on Windows
Git executable files are located in the C:\Program Files\Git\bin\git.exe
and C:\Program Files\Git\cmd
. These directories must be added to the PATH
environment variable to run the program.
Let’s explain step by step how to do this.
Step 1: Navigate to the System Properties on Windows
To access the environment variables, you have a couple of routes.
- Environment variables are located under the System Properties. We can navigate there by typing
edit environment variables
in the search box and clicking on the best match.
- Alternatively, follow these steps:
- Right-click on the
This PC
. - Select the
Properties
tab.
- Scroll and click on the
Advanced System Settings
.
Step 2: Edit Environment Variables to Add Values to the PATH
Variable
Click Environment Variables
under System Properties
to add values to the PATH
.
Select Path
under the System variables
and click the Edit
button.
Click the New
button and add the C:\Program Files\Git\cmd
value. Save it and repeat the same process for the C:\Program Files\Git\bin\git.exe
.
If your executable files are located in the C:\Users\<user>\AppData\Local\GitHub\PortableGit_<guid>\bin
and C:\Users\<user>\AppData\Local\GitHub\PortableGit_<guid>\cmd
directories, add them to the Path variable.
Save the changes and close the page.
Add git
to the PATH
on Windows using Batch Script
A batch script is a series of commands executed by the Windows Command Prompt. Here’s how you can create a batch script to add Git to the PATH:
Step 1: Create a Batch Script
-
Open a text editor such as Notepad.
-
Copy and paste the following lines into the text editor:
@echo off setlocal REM Specify your Git installation path set "gitPath=C:\Program Files\Git" REM Add Git to the PATH setx PATH "%PATH%;%gitPath%\bin;%gitPath%\cmd" /M echo Git has been added to the PATH. echo Please restart your command prompt to use Git.
-
Save the file with a
.bat
extension (e.g.,add_git_to_path.bat
).
Below is the explanation of the above code.
setlocal
: This command starts a local environment to contain variables, preventing them from affecting the system globally.set "gitPath=C:\Program Files\Git"
: Set thegitPath
variable to the path of your Git installation directory.setx PATH "%PATH%;%gitPath%\bin;%gitPath%\cmd" /M
: This line uses thesetx
command to modify the PATH environment variable. It appends Git’s bin and cmd directories to the existing PATH.
Step 2: Run the Batch Script
- Locate the saved batch script (
add_git_to_path.bat
). - Right-click on the script and select
Run as administrator
. This ensures that the script can modify system variables. - The batch script will execute, adding Git’s directories to the
PATH
. - After the script completes, you’ll see the messages indicating that Git has been added to the
PATH
. To apply the changes, restart your command prompt.
Add git to PATH
using PowerShell
Using PowerShell to add Git to the PATH is straightforward and can be accomplished in a few steps:
Step 1: Open PowerShell as Administrator
-
Open PowerShell as Administrator: Press Win + S, type
PowerShell
, right-click onWindows PowerShell
and chooseRun as administrator
. This ensures that you have the necessary permissions to make changes.
Step 2: Add Git to PATH
-
In the elevated PowerShell window, run the following command to obtain the path of your Git installation:
$gitPath = (Get-Command git).Source.Replace("git.exe", "")
Explanation:
(Get-Command git)
: This command retrieves information about thegit
executable..Source
: This property of the command’s output contains the full path to the executable..Replace("git.exe", "")
: This part of the command removes thegit.exe
portion from the path, leaving the directory path.
-
Now, run the following command to add Git to the PATH:
$env:Path += ";$gitPath"
Explanation:
$env:Path
: This environment variable contains the current PATH.+=
: This operator is used to add the Git path to the existing PATH.";$gitPath"
: This part of the command appends the Git path to the existing PATH.
Verification and Advanced Considerations
With Git integrated into the PATH, let’s verify its successful integration:
- Open a new Windows Command Prompt.
- Type
git --version
and press enter.
If you see the Git version information, congratulations, Git is now accessible from any command prompt window.
Conclusion
The process of adding Git to the PATH environment variable goes beyond mere technicality—it’s a gateway to efficient version control and seamless collaboration.
By following this comprehensive guide, you’ve learned how to integrate Git with your command prompt, enhancing your development capabilities.
Whether you’re a beginner or an experienced developer, this knowledge empowers you to harness the full capabilities of Git and excel in the world of software development.
Yahya Irmak has experience in full stack technologies such as Java, Spring Boot, JavaScript, CSS, HTML.
LinkedIn