Skip to main content

10 git aliases for a faster and productive git workflow

Written by:
Royee Shemesh

Royee Shemesh

December 2, 2020

0 mins read

Using git as a code versioning tool is a day-to-day activity for developers, and some of you may be practicing your git workflow through the command line. Preferably with a dark theme too, right?

Although a GUI for git might come in handy for an integrated development environment (IDE) such as IntelliJ, or VS Code, you may at times need to resort to the command line interface (CLI).

In this article, we’re featuring 10 git aliases that can help with a faster and more productive git workflow as an individual, or within a team.

Let the git games begin!

How to set a git alias?

Your git aliases are often stored per your user’s configuration at ~/.gitconfig. You can also manually set aliases using, for example, the command git config alias.s ‘status -s’.

Next, we’ll cover the git aliases, which you should add to your ~/.gitconfig file in a specific [alias] section, where all aliases and their commands are stored. Check out below some examples of git aliases configuration in that file.

How to list git aliases?

The git executable has a command line option to list all of git configuration and where they originate from (system, user, or local). The following git command will list all git configuration verbosely with their origins: git config --list --show-origin.

[alias]
    s = status
    co = checkout

Git status

Do you often run a git status command? I do! Use the following git alias to have a shortcut for it:

[alias]
    s = status

Here it is in action:

$ git s

On branch main
Your branch is up to date with 'origin/main.

nothing to commit, working tree clean

Git checkout

Moving back and forth between git branches is also something we often do. Let’s shorten that!

Add the alias:

[alias]
co = checkout

And then give it a try:

$ git co feat/add-popup

In fact, a git branch checkout can be done using a shortcut, such as git checkout - which checks out the previous branch you were on. It is actually a shorthand of git checkout @{-1} so this alias can use it as well using git co -.

Create a new branch and switch to it

If you find yourself often creating new git branches from the CLI, here’s one to save you some keystrokes:

[alias]
cob = checkout -b

Use it as follows:

$ git cob feat/add-popup

Switched to a new branch 'feat/add-popup'

Delete a branch

Removing a branch perhaps isn’t something you often do, but when you have to, it might be time-consuming to Google through git commands to find the command argument and flag it. Instead, we can create a shorter and more memorable git branch checkout-related alias:

[alias]
del = branch -D

Use it as follows:

$ git del feat/add-popup
Deleted branch featureB (was b5cbv113).

List all branches

When we work on multiple features, our git workflow usually ends up with multiple git branches as well. Finding where we last left-off and which branch we used could be challenging.

The following git alias will list all branches and sort them by commit date, showing the most recent git branch first, based on commits made to it.

[alias]

br = branch --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(contents:subject) %(color:green)(%(committerdate:relative)) [%(authorname)]' --sort=-committerdate

Here is how to use it and what the output might look like:

$ git br
wordpress-sync/blog-git-branch-code

As you can see, it provides more context, such as the name of the git commit author on each of the last commits, with colorful highlights.

Git commit

Committing always and early is one of git’s strengths. It is fast and adds no overhead due to the way git internals work. So, why not do it often?

It really depends on your specific git workflow—however, if you work on git workflows often and want to save your commits just as often (so you don’t lose track of work being done), the commit message text may be less of a priority, especially if you’re working within a pull request workflow.

To allow for a faster and more productive git workflow of saving commit status points, the following git alias is a quick one to add all git unstaged files into the staging area with a generic commit message.

[alias]
save = !git add -A && git commit -m 'chore: commit save point

Here is how it looks like in action:

$ git save

[feat/add-popup 98cba110acb] chore: commit save point
1 file changed, 1 insertion(+)

Rollback changes

If you find yourself, at times, needing to commit changes but then do a rollback, the following alias will roll all of your commit changes back into the local unstaged area, so you can make modifications and add them to the staging area (often referred to as the index).

[alias]
    undo = reset HEAD~1 --mixed

Once you invoke this git alias, use git undo to reset the previous commit on this branch, and check out all the previous committed changes as uncommitted, so you can resume work on them.

Use it as follows:

$ git undo
Unstaged changes after reset:
M app.js

Clean all changes

We all wish for a fresh start, don’t we? When in need of a quick clean up of all of your local changes which you wish to avoid committing to the repository at this stage, even locally, use the following alias:

[alias]
 res = !git reset --hard

It resets all staged changes (those that you added with the git add command). Here is how it looks like in action:

$ git res
HEAD is now at 6e7879bc81a chore: commit save point 

Push changes to upstream

By the time you finish working on the code, you send commits upstream to the git repository like all of us, right? We often do that using a lengthy command: git push origin or git push origin main.

However, if you’re working on the same branch name as the one you wish to push changes to, then we can alias the remote branch name as HEAD.  Push your commits from the local git repository to the origin or upstream remotes with a shortcut as simple as git done using this alias:

[alias]
    done = !git push origin HEAD

Using this alias and the shortcut of HEAD is often useful when the branch names are long. Here is how it is easily used:

$ git done

Enumerating objects: 42, done.
Counting objects: 100% (42/52), done.
Delta compression using up to 8 threads
Compressing objects: 100% (8/8), done.
Writing objects: 100% (8/8), 1.63 KiB | 7.63 MiB/s, done.
Total 42 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 8 local objects.
To github.com:royeeshemesh/git-workflow.git
   bc535183..1e42d90  HEAD -> main

Git log

To wrap up, we’re going to feature one of the most useful git commands: git log. There are many different ways of using git aliases to construct log output but my suggestion is to use the following customized git alias:

[alias]
    lg = !git log --pretty=format:\"%C(magenta)%h%Creset -%C(red)%d%Creset %s %C(dim green)(%cr) [%an]\" --abbrev-commit -30

This git alias will make git log print an output that is considerably more readable. Try it out:

$ git log
wordpress-sync/blog-git-log

Git aliases summary

We’ve iterated 10 git aliases to boost your productivity and for a faster git workflow, if you’re working in the command line.

Here is the complete list of git aliases to include in your ~/.gitconfig file:

[alias]
    s = status
    co = checkout
    cob = checkout -b
    del = branch -D    
    br = branch --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(contents:subject) %(color:green)(%(committerdate:relative)) [%(authorname)]' --sort=-committerdate
    save = !git add -A && git commit -m 'chore: savepoint'
    undo = reset HEAD~1 --mixed
    res = !git reset --hard
    done = !git push origin HEAD
    lg = !git log --pretty=format:\"%C(magenta)%h%Creset -%C(red)%d%Creset %s %C(dim green)(%cr) [%an]\" --abbrev-commit -30
What’s next?

What’s next?

We highly recommend you check out our 10 GitHub security best practices. While you’re at it, how about connecting your git repository to Snyk to find and fix vulnerabilities in open source dependencies in your projects?

Posted in:

Level Up Your CI/CD Pipelines

See how these 8 tips can help you catch security issues in the pipe BEFORE you push to production ⭐️