Git How to Create an Alias to Save Time

Git How to Create an Alias to Save Time

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

How to Search for a Lost File in the Git Log

How to Search for a Lost File in the Git Log

  • 2024-04-27
  • 4 minutes to read

I have lost a file in my Git repository. How can I find it by searching the git log?

Read More
No Such Shell Function 'Zle Line Init' in Zsh

No Such Shell Function 'Zle Line Init' in Zsh

  • 2024-04-25
  • 3 minutes to read

Troubleshooting the error message "no such shell function 'zle line init'" in zsh when using OhMyPosh.

Read More
Getting Started With Python in Vscode

Getting Started With Python in Vscode

  • 2024-04-05
  • 2 minutes to read

This post will help you get started with Python in Vscode and identify some of the useful extensions to install.

Read More