How to Set Bash Aliases for Git Commands on Windows

Personalize your terminal experience for improved workflow efficiency.

How to Set Bash Aliases for Git Commands on Windows

In this blog post, I’ll walk you through setting up Git command aliases in a Windows environment by creating a .bashrc file. This file allows you to add shortcuts and customizations, making your workflow more efficient. We’ll also cover some Git-specific aliases to speed up your daily tasks.

Step 1: Create a .bashrc File in Windows.

First, you’ll need to create a hidden .bashrc file in your user directory.

  1. Navigate to the directory where you want to create the file, typically:
C:\Users\[your-username]/

To create the file:

    • Open Notepad (or any text editor).

      • Go to File > Save As.

      • In the "File name" field, type .bashrc. (with the trailing dot). This is a trick to force Windows to save a file with no extension.

      • Save the file.

  1. The file should now be visible in your user folder as .bashrc.

Step 2: Add Aliases to C:\Users\[your-username]\.bashrc

Once the .bashrc file is created, it’s time to add some Git command aliases. These aliases act as shortcuts to common commands, helping you speed up repetitive tasks.

Here's how to add basic Git aliases to your .bashrc file, which you can modify as needed.

# Add your own aliases or change these as you like

# Git Aliases
alias gst='git status'          # Shortcut for 'git status'
alias gco='git checkout'        # Shortcut for 'git checkout'
alias gbr='git branch'          # Shortcut for 'git branch'
alias gad='git add .'           # Add all changes
alias gcm='git commit -m'       # Commit with a message
alias gps='git push'            # Push changes
alias gpl='git pull'            # Pull the latest changes
alias gdf='git diff'            # View differences
alias glg='git log --oneline'   # View a short log of commits

# General aliases
alias ls='ls -alh'              # Show detailed file listing
alias ll='ls -lah'              # List all files including hidden ones
alias clr='clear'               # Clear terminal screen

Each of these aliases represents a shortcut to a more complex command. For example, typing gst will run git status, and gco will run git checkout.

Step 3: Save and Activate the Aliases

After adding the aliases to the .bashrc file, save the changes. To apply these changes, open Git Bash and run the following command:

source ~/.bashrc

This command reloads the .bashrc file and applies the changes immediately. To ensure all changes take effect, close any existing terminal windows and reopen them before using the aliases.

Step 4: Verify the Aliases

To verify that your aliases are working, open your terminal and try running one of your new shortcuts. For example:

gst   # Should show 'git status'
gcm   # Use like this: gcm "Your commit message"

Congratulations! You’ve successfully set up your Git aliases.

Note: Setting up Git command aliases in Windows can save you a lot of time and effort. By following the steps in this guide, you can tailor your command-line experience to fit your workflow. Feel free to add, remove, or change aliases in your .bashrc file as needed to boost your productivity.