DEV Community

Cover image for Faster Git workflow with Git Aliases
Yankee Maharjan
Yankee Maharjan

Posted on

Faster Git workflow with Git Aliases

Git is an intricate part of our development workflow. There are handful of commands that you keep repeating day in and day out. I always relied on the command suggestions, or packages on top of my shell that gave access to handy git aliases.

But usually you had to stick with the aliases decided by the package creators . Although most of the aliases included are unofficially globally accepted like ga for git add and so on.

But guess what? You don’t have to rely on any third party packages ; you can create your own with the aliases you prefer!

There are two ways of creating Git aliases: ✌

with the git config utility 💻

This is the preferred way of adding aliases as git gives you an option to do so.

Suppose you feel tired of repeating the commit command every now and then. It would be nice if we could create an alias to write our commits faster.

Say no more! 🎉

git config --global alias.c "commit -m"
Enter fullscreen mode Exit fullscreen mode

The above command follows the following syntax:

git config --global alias.<alias> "<command>"
Enter fullscreen mode Exit fullscreen mode

Now we can use the c alias to represent commit -m .

git c "Update readme with social links"
Enter fullscreen mode Exit fullscreen mode

editing the .gitconfig file 📝

It would be tedious to add multiple aliases with the git config command, so there’s an easier alternate approach.

All the alias you create is saved to the .gitconfig file sitting in your home directory. We can open the file and add our aliases following the TOML formatting. Make sure you do not modify anything in the file apart from adding the [alias] table and its contents.

Open the file using your favorite editor.

vim ~/.gitconfig 
# or
code ~/.gitconfig
Enter fullscreen mode Exit fullscreen mode

Start adding your alias ✍️

...
[alias]
        st = status
        c = commit -m
        a = add
        cb = checkout -b
Enter fullscreen mode Exit fullscreen mode

You can use the above aliases as follows:

git st # git status
git c "hello world" # git commit -m "hello world"
git a hallucination.py # git add hallucination.py 
git cb multi-stage-build # git checkout -b multi-stage-build
Enter fullscreen mode Exit fullscreen mode

There’s also a handy command: git config --list to view the contents of the file including other git configurations.

Bonus 🛸

Git alias with parameters!

We can take our alias a step further by using shell script to add parameters.

Where do we benefit from the parameters?
Let’s take an example of adding a git remote to our local repository. There are basically two variables in the command that I can pass as a parameter, namely, the remote name and project name.

git remote add <remote-name> git@github.com:yankeexe/<project-name>.git
Enter fullscreen mode Exit fullscreen mode

We can represent the above abstraction using an anonymous bash function f()as follows:

[alias]
        ra = "!f() { git remote add $1 git@github.com:yankeexe/$2.git; };f"
Enter fullscreen mode Exit fullscreen mode

The $1 and $2 is the order in which the parameter will be used by the function. ! represents a shell script; our anonymous function is f() which we have invoked immediately at the end.
Let's use our alias:

git ra origin demo-project 
Enter fullscreen mode Exit fullscreen mode

Here origin will be used in $1 since it’s the first parameter we are passing and $2 will be demo-project.
The above command will translate to:

git remote add origin git@github.com:yankeexe/demo-project.git
Enter fullscreen mode Exit fullscreen mode

Conclusion 🚀

I hope this article has been of help in improving your Git workflow. If you have any queries or suggestions, let’s discuss in the comment section.

Discussion (7)

Collapse
abdurrkhalid333 profile image
Abdur Rehman Khalid

A Very Excellent Article for Persons Like Me Who Enjoy to Be a Kind of Funky with Git and All Other Things.

It is true that all other Text-Editors and IDEs have builtin Git support but it is always fun to work on the Shell looking and feeling like a hacker.

This gives the feel of having some special sort of superpowers.

Collapse
yankee profile image
Yankee Maharjan Author

awesome 🎉

Collapse
raspikabek profile image
Eduardo Asenjo

Even though this is something everyone can find and apply from the official documentation, always is great to have reminders like this post showing the potential and alternatives you can create just to make your work easier.

Thanks for this post! This was a revelation for me (also use some custom aliases in bash) and I've shared this post with friends and coworkers

Collapse
yankee profile image
Yankee Maharjan Author

Thank you ☺

Collapse
tbhaxor profile image
Gurkirat Singh

Did you try bashit? It has much aliases like gpo for git push origin HEAD :)

Collapse
icanhazstring profile image
Andreas Frömer

I am using Bash-it. With an alias for most of the tools I use.

github.com/Bash-it/bash-it

Collapse
awesomeironman profile image
Cyrus Gracias

I prefer working on Linux with bash Aliases for common git commands instead of this