Getting Started with Git Bash Commands on Windows

Published:1 June 2023 - 6 min. read

Ransomware recovery test drive: This technical workshop is designed to take you behind the scenes and shows you how to adopt strategies to automate recovery, ensuring you’re ready to become a recovery hero. REQUEST YOUR LAB

This article has been updated for 2023!

Did you know you can access Linux utilities directly on your Windows operating system? Perhaps you want a separate terminal to run Git and Linux commands. If so, learn more about Git Bash commands (Bourne Again Shell).

Not a reader? Watch this related video tutorial!
Not seeing the video? Make sure your ad blocker is disabled.

In this tutorial, you’ll learn about Git Bash commands, install Git Bash on Windows, and run some basic Git Bash commands in a terminal.

Prerequisites

The demonstrations in this tutorial are on a Windows 11 PC version 22H2, but prior versions of Windows will work.

What is Git Bash?

Before you look at what Git Bash is, let’s go over what Git is. Git is a version control system for controlling changes in software development. Like macOS and Linux, operating systems already have a command-line terminal where you can run Git and Linux commands directly. But for Windows, you have the Windows command prompt, a non-Unix terminal.

How can you run Git and Linux commands in Windows? Git Bash commands will do the trick. Git Bash commands are emulated, and that provides an emulation layer for Git to run Linux commands on Windows PCs. An emulator enables one specific system to behave like another computer system.

Downloading and Installing Git Bash

Now that you know a little about Git Bash, let’s see how you can download and install it. There are three different ways of downloading the Git software for Windows OS, as shown below.

Download Git in any of the three ways you prefer, but this tutorial will use the official website.

Git website to download the application.
Choose the appropriate version for your system.

After downloading Git, time to install Git on your Windows PC. More modern systems offer the ability for an unattended install of Git via WinGet.

1. Launch the installer you downloaded and click Next through the steps until you reach the Select Components screen.

2. Now, check the boxes of additional components you want to include in the installation. Leave the ones selected by default, as shown below, and click Next.

Selecting the appropriate components.

3. Leave the default for creating a shortcut in the start menu folder, and click Next.

Choose the correct start menu folder.

4. Select Use Notepad as Git’s default editor from the drop-down list as a default editor to use with Git, and click Next. Now Git files like ~./gitconfig will open in Notepad by default.

Selecting Git’s Default Editor

5. Select the Override the default branch name for new repositories option as the default branch name (main) for Git to use. When you initialize a Git repository, Git will use this branch name by default.

The default branch name used to be “master” for Git repositories. But many people found “master” an offensive word. So GitHub followed the Software Freedom Conservancy’s suggestion and provided an option to override the default branch name when initializing a Git repository.

Selecting Default Branch Name

6. Now, select Git from the command line and also from 3rd-party software option so that Git Bash command can be executed from different tools. Command Prompt, PowerShell, or any other 3rd party software tools, along with the Git Bash console, are some of those tools.

Selecting a Console Where Git Commands Can Run

7. Select the Use the OpenSSL library option to let Git validate certificates with OpenSSL, and click Next. OpenSSL is a cryptographic library containing an open-source SSL and TLS protocol implementation.

If you use Git in an organization with enterprise-managed certificates, select the User the native Windows Secure Channel library option instead.

Selecting SSL/TLS library for HTTPS connections

8. Leave the default Checkout Windows-style, commit Unix-style line endings option selected, and click Next.

If you configure “Windows-style” line-ending conversions, when you hit return on your keyboard after executing a Git command, Git will insert an invisible character called line ending. When different contributors make changes from different operating systems, Git might produce unexpected results.

Selecting the line endings option

9. Select the Use Mintty (the default terminal of MSYS2) option as the default terminal emulator to run commands, and click Next. Mintty is the default terminal of MSYS2. MSYS2 is a collection of tools and libraries that provides a Unix-like environment for software distribution and a building platform for Windows.

Selecting a default terminal emulator

10. Select the Default (fast-forward or merge) option below as the git pull command’s default behavior. The git pull command is the shorthand for git fetch and git merge, which fetches and incorporates changes from a remote repository into the current branch.

Perhaps you want to merge a new branch to the master. If so, Git would directly merge using fast-forward without going through git fetch and git merge commands. The merge is only possible if there are no commits on the primary branch (i.e., master or main) from when you’ve created the new branch.

Selecting Default Behavior for git pull Command

11. Select the Git Credential Manager Core as the default Git credential helper, and click Next. Git credential helpers are external programs that Git can prompt for input data, like usernames and passwords. These input data can be stored in memory for a limited time or on the disk.

Git Credential Manager Core is based on the .NET framework and will provide multi-factor HTTPS authentication with Git.

Selecting Default Credential Manager

12. Leave the extra features on default, as shown below, and click Next. The Enable file system caching option is checked to provide quick results when executing Git commands.

Enabling extra options

13. Ensure to leave both options below at default (pseudo console and built-in file system monitor) as they are still in an experimental stage, and click Install.

Experimental options support

14. Complete and close the installation wizard by clicking on Finish.

Git setup wizard complete screen

15. Right-click on your desktop and select Git Bash Here from the context menu below to launch Git Bash terminal. Launching Git Bash commands from your desktop is one of the quickest ways, but the same process goes when you right-click on a folder.

Choosing Show More Options
Launching Git Bash from Desktop Context Menu

16. Run the git command below to verify Git Bash commands are available and its current version (--version).

git --version

You can see below the current Git version in this tutorial is version 2.40.1.windows.1

Verifying Git Bash commands are available

Running Git Bash Commands in Different Terminals

Now that you have Git Bash on your PC, it’s time to learn some Git commands. Running Git Bash commands isn’t limited to Git Bash console only. Did you know you can run Git commands in the command prompt too? Yes!

Let’s run Git commands both on Git Bash console and command prompt to declare variables accessible in both terminals.

1. Launch Git Bash console by clicking on the Start button, type git, and click on Git Bash.

Launching Git Bash commands from the Start menu

2. Run the below git config command to add your name (YourName) as your git username (user.name). The git config command administers configuration variables that control how Git looks and operates.

Pass the --global option to the git config command to define the configuration variable (YourName) in the ~/.gitconfig file specifically.

git config --global user.name "YourName"

3. Now open the command prompt and run the below git config command to add your email ("[email protected]") as your git user email (--global user.email) in the ~/.gitconfig file.

git config --global user.email "[email protected]"

4. In the same command prompt window, run the below git config command to list (--list) all the configuration variables in Git.

git config --list

You can see below that even though you’ve added variables in the ~/.gitconfig file via different consoles. The variables are accessible and displayed in the command prompt.

Viewing Global Variables Added via Different Consoles

Running Linux Git Bash Commands

As you can tell, all Git Bash commands work in both Git Bash and the command prompt. And since Git is delivered as a Unix-style command-line environment, let’s try running a Linux command on the Git Bash console!

Run the ls command both in Git Bash console and command prompt to list the files and folders in the working directory.

The screenshots below show that the Git Bash command console returns an output while the command prompt throws an error, saying the ‘ls’ command is not recognized.

Running Linux Command in Git Bash Returns a Result
Running Linux Command in Command Prompt Returns an Error
Running Linux Command in Command Prompt Returns an Error

You can run Linux commands on the command prompt if you first change the directory to C:\Program Files\Git\usr\bin.

In the command prompt, run the commands below to change the working directory to C:\Program Files\Git\usr\bin and run the ls command.

cd C:\Program Files\Git\usr\bin # Change directory to where Linux utilities are stored
ls # Linux command that lists all files and folders in the working directory

Below, you can see that you didn’t get an error after running the ls command this time, but the command returned results instead.

Running Linux Commands in Command Prompt

Conclusion

This tutorial explains what Git Bash commands are and the steps for Git software installation. You’ve learned what Git Bash is and that running Git commands both on Git Bash and the command prompt is possible.

Next time you run Git commands, which console will you turn to?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!