How to create a git alias to run common or custom commands

How to create a git alias to run common or custom commands

Table of Contents

A long time ago in a galaxy far, far away…

This post is over 12 months old, that's a long time in tech! Please be mindful of that when reading this post, young Padawan, as it could be outdated. I try to keep things up to date as much as possible. If you think something needs updating, please let me know in the comments.

Introduction

Git aliases allow you to set up regularly used, or complex commands to shorter keywords. Once you have set up an alias you simply call your alias using git (for example git foobar and git will then call your desired command.

I wanted to find a command to search local branches for “unpushed” changes, a quick google later found me this command;

 # ref https://stackoverflow.com/questions/39220870/in-git-list-names-of-branches-with-unpushed-commits
 git log --branches --not --remotes --no-walk --decorate --oneline

But I won’t remember that command after I have slept. Setting this command up as an alias will make it much easier to remember. I am going to set up an alias called “unpushed” and when I call it by typing in “git unpushed” git will run the git log command.

Syntax for setting up an alias

The syntax for setting up an alias looks like this;

git config --global alias.{your} "{command}"

Where {alias} is the command you will write and {command} is the actual command that will be run. So based on the above, I need to run the following;

git config --global alias.unpushed "log --branches --not --remotes --no-walk --decorate --oneline"

Make sure to quote the command!

Checking the config

Running this command won’t give any feedback, but I can check it’s been set by reviewing my config file. I can do that by running git config --global --list;

git config --global --list

filter.lfs.required=true
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
user.name=Justin Bird
user.email={redacted}
alias.unpushed=log --branches --not --remotes --no-walk --decorate --oneline

You can see the last line is my new alias.

Using the alias

So now if I run git unpushed, git will run the command git log --branches --not --remotes --no-walk --decorate --oneline and show me a list of branches that have unpushed commits.

git unpushed
abdf8f8 (demo/branch2) dummy change
a9f082d (demo/branch1) dummy change
4752671 (HEAD -> main) draft of function set up

My alias worked! I can check over my repositories for uncommitted changes across all branches using an easy to type / remember command.

How to set an alias manually

Alternatively you can fire up your config file and set the alias manually. I have had to do this recently to apply my deleteMerged command correctly. Using the process above, I had some challenges with getting it to set the commands correctly and whilst I could have spent more time trying to get it to work, I decided to just edit the config file directly. To find out where your config file is located, run the following command;

git config --list --show-origin

You can then open the file and add the alias manually.

Summary

I have found a really useful command, it’s not one I am going to remember very easily. I have configured an alias in the global config file which makes things much easier to remember! More details on aliases in the Git help.

#mtfbwy



Recent Posts