Git is undeniably powerful, but it can sometimes feel verbose and time-consuming. Especially for commands you type in multiple times a day. That's where Git aliases come in. They're nothing more than custom shortcuts, but they can significantly streamline your workflow and boost your productivity.
This article will introduce you to 10 Git aliases that every software engineer should consider. We'll dive into what each alias does, how to set it up, and how it can make your daily Git interactions smoother and more efficient.
Time to Git gud.
What Are Git Aliases?
A Git alias is a custom shortcut for a Git command. It lets you create your own Git subcommand that can be used just like the built-in ones.
For example, instead of typing git commit -m "Your message"
, you can set up an alias to simply type git cm "Your message"
. Perhaps a small change as a one-off, but when you have several of these that you use multiple times a day, it adds up to a big difference over time.
How to Set Up a Git Alias
You have two ways to set up a Git alias:
- Using the command line:
git config --global alias.<alias-name> '<git-command>'
- Editing your
~/.gitconfig file
. Add aliases under the[alias]
section withalias-name = git-command
.
1. Shorthand Status: Your Repo at a Glance
Setup: git config --global alias.s 'status -sb'
Usage:git s
A simple one to begin with. This Git alias gives you a concise, but informative status of your working directory. The -s
flag gives you a short-format output, while the -b
flag shows the branch and tracking information.
It can be a big time-saver when you check the status of your repository a lot. Instead of having to type a full git status
output, you get a quick overview of your current branch and its relationship to the remote, the modified files, and untracked files.
2. Enhanced Log: A Visual History
Setup: Put the below in your ~/.gitconfig
file.
lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<<%an>>%Creset' --abbrev-commit --date=relative
Usage: git lg
This transforms your Git log into a visually appealing graph. Let's dissect the setup:
--graph
: Draws an ASCII graph showing the branch and merge history.--pretty=format:'...'
: Customizes the output format. The format string uses these placeholders:%h
: Abbreviated commit hash%d
: Ref names (branches, tags)%s
: Commit subject (first line of commit message)%cr
: Committer date, relative%an
: Author name
%C...
and%Creset
: Color codes to improve readability--abbrev-commit
: Shows only the first few characters of the commit hash--date=relative
: Shows dates in relative format (e.g. 2 days ago)
This alias is particularly useful when you want to understand the branching structure of a project, want to quickly identify recent changes and their authors, or when you want to get a high-level overview of your project's history. The visual nature makes it much easier to grasp the flow of a project (as opposed to the standard git log
output).
3. Quick Commit: Streamline Your Commit Process
Setup: git config --global alias.cm 'commit -m'
Usage: git cm 'Your commit message'
A simple alias that saves you from typing git commit -m
every time you want to make a commit with a message. The -m
flag allows you to specify the commit message directly on the command line.
It may seem like a trivial alias, but it's one that can speed you up a lot if you're following the best practice of making small, frequent commits. It reduces the friction in the commit process and encourages more regular commits.
4. Undo Last Commit: Your Safety Net
Setup: git config --global alias.undo 'reset HEAD~1 --mixed'
Usage: git undo
This alias "uncommits" your last commit. The HEAD~1
refers to the commit before the current HEAD. It's essentially "one commit before HEAD". And the --mixed
flag resets the index but not the working tree. This means that it undoes the last commit and unstages changes, but the changes remain in your working directory.
Suffice to say that this alias is invaluable when you've made a mistake in your last commit. Instead of making a new commit to fix the error, you can undo the commit, make your changes, and then recommit. It keeps your commit history clean and logical. Especially useful for typos or when you forget to include a file.
5. Stash and Apply: Juggling Tasks
Setup: staa = !git stash apply && git stash drop
(add this to your ~/.gitconfig
file)
Usage: git staa
This alias combines the stash apply
and stash drop
command. The former applies the latest stash to your working directory, while the latter removes it. The &&
ensures that the drop only happens if the stash is successful.
It will streamline your workflow if you frequently use stashes to switch between tasks. Instead of manually applying a stash and then dropping it, you can do both in one command. Particularly useful when you want to apply changes from a stash without keeping a stash entry around.
Edit: An X-Teamer let me know that there's such a thing as "git stash pop", which equally applies and then drops a stash. So you can use either the Git alias or git stash pop.
6. Amend Commit: Polish Your Last Commit
Setup: git config --global alias.amend 'commit --amend --no-edit'
Usage: git amend
This lets you modify your last commit without changing its message. The --amend
flag replaces the tip of the current branch with a new commit, while the --no-edit
flag reuses the previous commit message without launching an editor.
Useful when you need to make small changes to your previous commit, but don't want to undo it altogether. Think typos or bugs that don't warrant a separate commit. Also useful to tweak your code style after committing. It keeps your commit history clean, without "oops" commits or fixes.
7. Show Staged Changes: Review Time
Setup: git config --global alias.staged 'diff --cached'
Usage: git staged
This Git alias shows you the changes in the staging area that will be included in the next commit. The --cached
flag (equivalent to --staged
) is important, as it tells Git to show the differences between the index and the last commit.
A pretty important one for keeping commit hygiene. By quickly reviewing your staged changes, you can make sure you're only committing what you intend to. More often than not, you'll catch unintended changes before they make it into a commit. Always keep things focused and atomic.
8. Interactive Rebase: Rewrite History with Precision
Setup: git config --global alias.ri 'rebase -i'
Usage: git ri HEAD~3
A shorthand for interactive rebase. The -i
flag stands for interactive and will open an editor to modify, squash, or drop commits. It's a powerful tool for cleaning up your commit history before pushing changes or submitting a pull request. It lets you:
- Squash multiple commits into one
- Reorder commits for a more logical progression
- Edit commit messages to provide better descriptions
- Drop commits that are no longer needed
For example, HEAD~3
lets you interactively rebase the last three commits.
9. Push to Current Branch: Simplify Your Push
Setup: pc = !git push origin "$(git symbolic-ref --short HEAD)"
(add to your ~/.gitconfig
file)
Usage: git pc
This alias pushes the current branch to a branch of the same name on the remote named "origin". It uses the subcommand git symbolic-ref --short HEAD
to get the name of the current branch.
This simplifies the process of pushing your current branch, especially if you're working with feature branches that might have long or complex names. Instead of typing git push origin feature/long-branch-name
, you can simply use git pc
. It will help reduce the chances of typos in branch names.
10. List Aliases: Never Forget Your Shortcuts
Setup: aliases = !git config --get-regexp ^alias\\. | sed -e s/^alias\\.// -e s/\\ /\\ =\\ /
(add to your ~/.gitconfig
file)
Usage: git aliases
Who doesn't love some regex? This alias uses a shell command to list and format all your git aliases. git config --get-regexp ^alias\\.
lists all config entries that have "alias." in them, while the sed
command formats the output for better readability.
This alias is useful because, once you've become addicted to Git aliases, it can be easy to forget which ones you've set up. It's is a quick way to remind yourself of your shortcuts. Also useful if you want to share them with a colleague.
Conclusion
These Git aliases were just the tip of the iceberg. Ultimately, the best Git aliases are shortcuts for the commands you use most frequently. A Git alias is so easy to set up it's almost a sin not to. Become comfortable with Git aliases and, before you know it, you'll find yourself working faster and more efficiently in Git.
TABLE OF CONTENTS