My Favorite GitBash Aliases

Published: August 14, 2023

The .bashrc file

A .bashrc file is a configuration file for the bash shell - which you'll be using if running GitBash. The dot at the beginning means it is a hidden file. To access the file:

  • In GitBash, run cd ~ which will take you to your home directory
  • Run ls -lah to see all the files (even hidden ones) in the directory
  • Look for one called .bashrc
    • If it does not exist, create it with touch .bashrc
  • Open it in a text editor of your choice

To add an alias

If you find yourself typing something lengthy a lot, you can create an 'alias'. For example, if you have to run winpty python everytime you want to run a python shell, you might wish that you could just use python.

The anatomy of an alias definition is : alias <what-i-want-to-use>='<the-actual-command>'

  • eg. alias node='winpty node' (genuinely useful)
  • eg. alias npm='winpty npm' (genuinely useful)
  • eg. alias python='winpty python' (genuinely useful)
  • eg. alias pip='winpty pip' (genuinely useful)
  • eg. alias showstuff='ls' (genuinely not useful)
💡

After making changes: Make sure you save the file and reload GitBash before trying out your shiny new aliases - or any other changes/additions you may make in this file!

Git Shortcut Alias

alias gacp='f(){ git add . && git commit -m "$1" && git push; }; f'

The given Bash alias is a handy shortcut for developers who regularly use Git, a popular version control system. In a typical development workflow, you might find yourself repeating several Git commands to add changes, commit them with a message, and push them to a remote repository. This alias bundles those commands into a single, concise command: gacp.

The alias gacp defines a function f and then immediately calls it. Here's a breakdown of the commands inside the function:

  • git add .: Adds all the modified and new files in the current directory and its subdirectories to the staging area. This prepares the changes to be committed.
  • git commit -m "$1": Commits the changes that have been added to the staging area with a message. The message is taken from the first argument you pass to gacp. For example, if you run gacp "My commit message", the commit message will be "My commit message".
  • git push: Pushes the committed changes to the remote repository.

The commands are linked using the && operator, so each one must succeed for the next one to run. If any command fails, the whole command will stop, allowing you to fix the issue before proceeding.

Python Anaconda Alias

While Python is a versatile programming language that can be used for a wide range of applications, Anaconda provides a more specialized environment for machine learning and data science. I use Anaconda to manage my environments, and sometimes I want to quickly activate my base environment. This alias allows me to do that with a single command:

alias condaon="source C:/Users/Shav/anaconda3/Scripts/activate"

The given alias, condaon, uses the source command to activate an Anaconda environment located at a particular path. For a closer look:

source

This command is used to execute the file in the current shell, allowing any changes (such as setting environment variables) to affect the current session.

C:/Users/Shav/anaconda3/Scripts/activate

This is the path to the activation script for the Anaconda environment. Running this script sets up the environment variables and other settings specific to Anaconda, allowing you to use the tools and libraries installed within that environment.

If you often switch between different projects that require various libraries or specific versions of Python, this alias can help you activate your base Anaconda environment quickly.