Git to the point - Git aliases.

grantdotdev

Grant Riordan

Posted on June 26, 2022

Git to the point - Git aliases.

As a dev, Git is just one of those things you'll interact with daily. But I'm sure you've all been there, where you can't remember those commands or it just gets a bit monotonous typing in the same long commands over and over again.

Well, this is where Git aliases can come in handy.

What is a Git Alias?

A git alias is a shorthand version of a command / custom command defined by yourself, which then in turn runs the real command. This make remembering those git commands less important, and I can guarantee it will make your day to day development a lot easier, and more efficient.

How to add git aliases?

This is quite simple. There are a couple of ways, but for me the easiest is simply to open up your terminal and enter the following command to edit your global Git config. Not only is this easier because you're able to add them in your editor of choice, but it also means that you have more space to view all your aliases.
You **can **add them via the terminal directly but I feel the syntax / view can be cumbersome (especially with more complex aliases).

How you'd do it in the terminal:

git config --global alias.co checkout
Enter fullscreen mode Exit fullscreen mode

My preferred way

But this tutorial is going to show you how to do it via my preferred way, via the Git config and editor.

git config --global --e
Enter fullscreen mode Exit fullscreen mode

This will open up your git config.

Once you have your editor open you can add an alias with the following code (you may see an alias section already there).

[alias]
    co = checkout
Enter fullscreen mode Exit fullscreen mode

This is just an example, but this will create an alias of co which in turn will be like running git checkout.
You can add your branch name in just the same way, like so:

git co feature/making-aliases
Enter fullscreen mode Exit fullscreen mode

Ok - Let's look at some of my favs and most used

Alias Setup:

tidy-up = !git branch | grep -v "main" | xargs git branch -D
Enter fullscreen mode Exit fullscreen mode

This is one of my favs, although I don't use it very regularly (I should though). How this works is simple, it will run
git branch which will return the list of local branches. It will then utilise the pipe mechanic (which in simplest terms will pass the result to the next command).

So we pass the branches to
grep -v "main" which will search the branch names, but the -v will exclude the branch main.

It then passes these to the next command (xargs coverts input to a command argument) to the git branch -D, (the delete branch command).

Git Status

Alias Setup:

s = status 
Enter fullscreen mode Exit fullscreen mode

Get the status of your current branch, it lets you see which changes have been staged, which haven't, and which files aren't being tracked by Git.

Git Latest

Alias Setup:

latest = !git checkout main && git pull
Enter fullscreen mode Exit fullscreen mode

This alias is very handy when u want to make sure your main branch is up to date before branching off, or if you need to check a bug on the main branch.

simply call git latest and then bam! you have the latest main code.

Git Checkout

Alias Setup:

co = checkout
Enter fullscreen mode Exit fullscreen mode

This is just a quicker, nicer way of checking out a branch.

Usage:
git co <branchName> ie. git co bug/defect101

Git checkout branch

Following on from git checkout, this goes a little further to create a new branch from the current branch

Alias Steup:

cob = checkout -b
Enter fullscreen mode Exit fullscreen mode

Usage:
git cob feature/new-feature

Git New-Work

I created this alias 'cos I was tired of having to go get main, git pull, and then branch off of the latest main.

Alias Setup:

  new-work = !git checkout main && git pull && git checkout 
Enter fullscreen mode Exit fullscreen mode

This will pull the latest code of the main branch, and create a new branch with the given name.

Usage:

git new-work bug/fix-issue

Isn't that a lot quicker / easier?

Git Branch - with a twist:

Alias Setup:

br = branch --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(contents:subject) %(color:green)(%(committerdate:relative)) [%(authorname)]' --sort=-committerdate
Enter fullscreen mode Exit fullscreen mode

Let's take a look at what this is doing.. this will

List all branches,
Branch name first
name of last commit
then how many days since the last commit & author name in green text
Ordering the list by most recently committed.

Usage:
git br

Output:
It will output something like this:
Image of coloured branch details in terminal

Git Save

This is another time-saver, where you just want to get everything you've worked on committed, tracked and untracked.

save = !git add -A && git commit
Enter fullscreen mode Exit fullscreen mode

This simply stages all the untracked and tracked changes. Then commits the changes without a message, which will cause your chosen git editor to open, allowing you to add a commit message + optional description.

You could also add another variation of this with:

saveM = !git add -A && git commit -m
Enter fullscreen mode Exit fullscreen mode

This is just like the git save but requires you to provide a commit message.

Git Done!

Ever been in that situation where you've gone to push your code and you get the prompt there's no remote branch and you see a message saying to use the command

git push --set-upstream origin <branchName>

Well, you can bypass this using the following git alias:

done = !git push -u origin HEAD
Enter fullscreen mode Exit fullscreen mode

This will push the current branch with the branch name to the origin (remote server).

so by the end, your git config could look something like this:

[alias]
    co = checkout
    cob = checkout -b
    s = status
    tidy-up = !git branch | grep -v "main" | xargs git branch -D
    latest = !git checkout main && git pull
    new-work = !git checkout main && git pull && git checkout  
    done = !git push -u origin HEAD
    save = !git add -A && git commit
    saveM = !git add -A && git commit -m
    br = branch --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(contents:subject) %(color:green)(%(committerdate:relative)) [%(authorname)]' --sort=-committerdate

Enter fullscreen mode Exit fullscreen mode

What do the symbols in the commands do?

&& - This simply chains commands together, but the right side command will only run if the left side doesn't occur any errors.

| - This will pass the output of the previous command to the right-side command.

Why do some of the alias have ! in front of them and others do not?

Well, adding an exclamation mark (!) at the beginning is letting the terminal know that it's running a full shell command, not a git subcommand such as (checkout, status etc). Ones that don't have the exclamation mark are a "sub-command" (pre-existing) of git.

The alias is replaced by its definition following the git command. Anything after the alias on the command line comes after the definition

I hope this was helpful, and you've learnt something from it all -- go forth and Git it done :-) -- always if you want to chat, or discuss drop feedback in the comments, or follow me on twitter.

💖 💪 🙅 🚩
grantdotdev
Grant Riordan

Posted on June 26, 2022

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

Sign up to receive the latest update from our blog.

Related

Git to the point - Git aliases.
productivity Git to the point - Git aliases.

June 26, 2022