How to Add Git to PATH on Windows

Yahya Irmak Feb 02, 2024
  1. Understanding the Crucial Role of PATH
  2. Manually Add git to the PATH on Windows
  3. Add git to the PATH on Windows using Batch Script
  4. Add git to PATH using PowerShell
  5. Conclusion
How to Add Git to PATH on Windows

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.

  1. 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.

search edit environment variables

  1. Alternatively, follow these steps:
  1. Right-click on the This PC.
  2. Select the Properties tab.

pc properties

  1. Scroll and click on the Advanced System Settings.

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.

environment variables

Select Path under the System variables and click the Edit button.

edit system variables

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.

add git to path

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

  1. Open a text editor such as Notepad.

  2. 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.
    
  3. 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 the gitPath variable to the path of your Git installation directory.
  • setx PATH "%PATH%;%gitPath%\bin;%gitPath%\cmd" /M: This line uses the setx 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

  1. Locate the saved batch script (add_git_to_path.bat).
  2. Right-click on the script and select Run as administrator. This ensures that the script can modify system variables.
  3. The batch script will execute, adding Git’s directories to the PATH.
  4. 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

  1. Open PowerShell as Administrator: Press Win + S, type PowerShell, right-click on Windows PowerShell and choose Run as administrator. This ensures that you have the necessary permissions to make changes.

    Start Windows PowerShell as Administrator

Step 2: Add Git to PATH

  1. 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 the git 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 the git.exe portion from the path, leaving the directory path.
  2. 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:

  1. Open a new Windows Command Prompt.
  2. Type git --version and press enter.

check git version on cmd

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.

Author: Yahya Irmak
Yahya Irmak avatar Yahya Irmak avatar

Yahya Irmak has experience in full stack technologies such as Java, Spring Boot, JavaScript, CSS, HTML.

LinkedIn