Git How to Create an Alias to Save Time

Git How to Create an Alias to Save Time

Table of Contents

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 here .

Tags :

#mtfbwy



Recent Posts

Hugo Live Reload Keeps Reloading

Hugo Live Reload Keeps Reloading

  • 2024-05-18
  • 3 minutes to read

I have run into an issue from time to time where Hugo's live reload feature keeps reloading the page. This is how I fixed it.

Read More
First Steps with Mermaid in Vscode

First Steps with Mermaid in Vscode

  • 2024-05-17
  • 7 minutes to read

My blog theme supports mermaid, a visual diagram syntax, so I wanted to try it out in vscode.

Read More
T-SQL Tuesday #174 My favourite job interview question

T-SQL Tuesday #174 My favourite job interview question

  • 2024-05-14
  • 4 minutes to read

This is my 12th contribution to TSQL Tuesday, the data community blog party. Have a read, and why not join in next time!

Read More