Faster Git workflow with Git Aliases

yankee

Yankee Maharjan

Posted on July 25, 2020

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.

💖 💪 🙅 🚩
yankee
Yankee Maharjan

Posted on July 25, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related