Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Git & GitHub - Git Aliases

How to create and use Git aliases

Git aliases allow you to create shortcuts for frequently used Git commands, making your workflow more efficient. This guide explains how to create and use Git aliases, including some common examples.

Key Points:

  • Aliases are custom shortcuts for Git commands.
  • You can create aliases for single commands or complex sequences of commands.
  • Aliases are configured in the Git configuration file.

Creating a Git Alias

To create a Git alias, use the git config command followed by the alias name and the command it should represent.


# Create a simple alias
$ git config --global alias.st status

# Create an alias for a complex command
$ git config --global alias.co checkout
                

This example creates an alias st for the status command and co for the checkout command.

Using Git Aliases

Once you have created an alias, you can use it as you would a regular Git command.


# Use the alias for status
$ git st

# Use the alias for checkout
$ git co branch-name
                

Listing All Aliases

To view all the aliases you have configured, use the git config --get-regexp alias command.


# List all configured aliases
$ git config --get-regexp alias
                

Editing Git Configuration File

You can also manually edit the Git configuration file to add, modify, or delete aliases. The configuration file is located at ~/.gitconfig for global aliases or .git/config in your repository for local aliases.


# Open the global Git configuration file in a text editor
$ nano ~/.gitconfig
                

Add or modify aliases under the [alias] section:


[alias]
    st = status
    co = checkout
    br = branch
    cm = commit -m
                

Common Git Aliases

Here are some common Git aliases that can help streamline your workflow:

  • st = status: Shortcut for git status
  • co = checkout: Shortcut for git checkout
  • br = branch: Shortcut for git branch
  • cm = commit -m: Shortcut for git commit -m
  • df = diff: Shortcut for git diff
  • lg = log --oneline --graph --decorate --all: Pretty log

Removing Git Aliases

If you want to remove an alias, use the git config --unset command followed by the alias name.


# Remove an alias
$ git config --global --unset alias.st
                

Summary

This guide covered how to create and use Git aliases to streamline your workflow. By configuring aliases for frequently used commands, you can save time and increase your efficiency when working with Git.