Extra Superior Git Aliases

Within the final article on this sequence, Superior Git Aliases, we took a take a look at some superior aliases for Git. Nevertheless, the true energy of Git aliases comes from writing customized scripts. These permit you to construct Git instructions that may do something you possibly can think about.

On this article, I am going to present you how one can create script aliases with Git. We’re going to check out a number of superior scripts you need to use for Git which can be tremendous helpful and can prevent a bunch of time.

Script Aliases

We’ll be utilizing Bash for our scripts. Bash is an unsightly language, but it surely has the good thing about working virtually wherever. To your personal scripts, you need to use any scripting language you need.

In the event you’re not aware of Bash scripting, do not panic! Whereas a number of the syntax might look funky, you possibly can largely muddle your approach by way of. (That is not one thing you usually hear from a coding article, proper?) I like to recommend studying Be taught Bash in Y Minutes and rifling by way of Google or Stack Overflow for something that does not make sense.

Step one is to create a file on your script. I wish to retailer these information in my Dotfile’s bin listing, however you possibly can put them wherever you need in your laptop. Simply be sure it is someplace simple to recollect.

contact bin/git/git-example

Subsequent, you may want so as to add some boilerplate to the highest of your script.

#!/usr/bin/env bash

set -euo pipefail

The primary line known as a shebang), and it tells your laptop that the file is a script that must be run with Bash. This particular runs the script with Bash. To make use of a unique language, substitute bash for one thing else, like ruby or python3.

The second line tells bash to exit if there are any errors, undefined variables, or errors in a pipeline. I do not know why this is not the default in Bash, however a minimum of it is easy to arrange with this line.

You possibly can run your script as-is with (bash bin/git/git-example), however who has time to write down out bash each time they need to run one thing? As a substitute, make your script executable.

chmod +x bin/git/git-example

Now you possibly can run your script with out prepending bash to it (e.g. bin/git/git-example).

Lastly, it is best to add your script to Git’s aliases. Substitute the trail on your script beneath.

  instance = "!$HOME/.dotfiles/bin/git/git-example"

That is it! Now you possibly can run git instance to run a script that does not do something!

Checklist All Branches

By default, whenever you run git department, you get the branches you have got saved regionally. However what if you wish to see all of the branches obtainable to you? You possibly can obtain this by including the --all flag to your branches.

git department --all

I wish to package deal this right into a git-branches script and add a number of extras to it.

#!/usr/bin/env bash

set -euo pipefail

# Solely output colour if the command is not being piped.
if [ -t 1 ]; then
  COLOR="all the time"

git department 
  "[email protected]" 
  | column -s ";" -t

Do not forget to avoid wasting this to a file and make it executable!

This does a number of issues:

  • It solely outputs colour when the script is being run immediately from the CLI. This lets you use git-branches in different scripts.
  • It types the branches by once they have been authored. This places the newest branches on the backside.
  • It means that you can cross further arguments to the department command utilizing the [email protected] Bash variable. It will are available helpful within the my-branches command we’ll add subsequent.
  • It provides some good formatting to your branches. For instance, for this reason my branches output seems to be like in my dotfiles repo. This works through the use of a trick with the column command and changing semicolons within the output so the gadgets line up properly.

Add an alias for this command (and a brief alias in case you like brevity).

  branches = "!$HOME/.dotfiles/bin/git/git-branches"
  bs = branches

You are all set! Now you can run git branches (or simply git bs) to see all off of the obtainable branches.

Checklist My Branches

The branches command you simply added could be very useful, however whenever you’re working with a big crew, it may be a ache to see everybody’s branches. I wish to create a second alias that solely consists of my branches. You possibly can simply accomplish this with a brand new script.

#!/usr/bin/env bash

set -euo pipefail

# Solely output colour if the command is not being piped.
if [ -t 1 ]; then
  COLOR="all the time"

"$HOME/.dotfiles/bin/git/git-branches" --color="$COLOR" | grep "$(git config person.identify)"

This script runs git-branches after which pipes the output by way of grep to filter it right down to the present person’s branches.

Create aliases for each of those instructions.

  my-branches = "!git branches | grep -i '$()'"
  my-bs = my-branches

You may scale back the quick alias to git mbs, however I do not as a result of writing git my-bs makes me smile each time I run it.

Stash Staged

Git has a git stash command, which is beneficial for setting apart work for later. By default, it solely stashes tracked information. You probably have new information you need to stash new information, it’s important to embrace the --include-untracked flag, or use git stash --all.

What do you do in case you solely need to stash some of your information? The built-in approach to do that is Git funky.

To illustrate you need to stash the information apple and banana, however preserve cherry and date. To do this, you add the information you do not need to stash to the index, after which run git stash --keep-index --include-untracked.

git add cherry date
git stash --keep-index --include-untracked

That is unusual as a result of it is the precise reverse approach that git commit works. Plus, you now have a few information floating round in your index that you will have to run git restore on.

To repair this, let’s create a git stash-staged command.

#!/usr/bin/env bash

set -euo pipefail

git diff --staged --name-only | xargs git stash push "[email protected]" --

That is it! This command makes use of git diff --staged --name-only to print out an inventory of all the information which can be within the index. Then, it pipes these arguments to xargs. xargs splits up the arguments by newlines and passes them to git stash --.

Add your alias, and also you’re finished!


You certain have been writing lots of aliases these days. Would not or not it’s good if there was a command we may run to record all the aliases you’ve got created? We are able to add an alias for that!

  aliases = "config --get-regexp alias"

That is All For Now!

That is it! Hopefully, you’ve got loved this text, and you may make good use of the aliases right here.

Do you have got a favourite Git alias? Let me learn about it down within the feedback!

Landon Schropp

About Landon Schropp

Landon is a developer, designer and entrepreneur based mostly in Kansas Metropolis. He is the creator of the Unraveling Flexbox. He is enthusiastic about constructing easy apps folks love to make use of.