Git Aliases Setup and Workflow
Git Aliases Setup and Workflow

In this video I want to talk about git aliases, what aliases are people using to be more efficient with git and what aliases I'm using based on my 10 years development experience.

So the first question is what are aliases in git at all? You can create a short commands on different git methods to simplify working with git. You set this commands you can either write them directly in global .gitconfig file in alias section on your machine or with a command

git config --global checkout

So normally all people are starting with this 4 aliases

git config --global checkout
git config --global branch
git config --global commit
git config --global status

Now you can write git co to checkout or git ci for commit. And it is already better than typing whole commands every time.

But of course after some time you want to make more different aliases and be even more efficient. So you are going to create aliases that are working best for you and no one else. It's actually goes in direction of improving commands that you are using in terminal at all.

So let's look on commands that I use to work with git and which are comfortable for me. When I created some git aliases after some time I understood the main problem with them. You need to start every command with work git and you can make just a command in console that you want.

This brings us to the mix of git aliases and aliases of your shell. I'm using zsh so I will show you my commands in zsh but you can do the same in bash or fish shells also. The main idea here is that you can create just any aliases which are not specifically git related. But of course in this video we will talk only about things that I use for git.

So first of all I create an alias for git. It's just b letter.

alias g="git"

I don't use it that often, only when I'm typing some git commands that I don't have in aliases.

alias gs='git s'

So to get a git status I just write gs. But as you can see on the right there is no such command as git s. This means that I created a git alias for it first.

s = status --short

So this is my alias inside git aliases. As you can see I have an additional attribute short to make the output less verbose.

The next one which I use really often is

alias gc='git commit -am'

As you can see it's not just short form of git commit. I also have the -am parameter. So my normal usage when I make commit is gc "Something changed". It adds all tracked files to commit and allows me to write a message directly.

Now here are git pull and push. Git pull is not interesting because it is just a wrapper but git publish command is super useful and it's an additional alias.

alias gpl='git pull'
alias gp='git publish'
branch-name = "!git rev-parse --abbrev-ref HEAD"
publish = "!git push -u origin $(git branch-name)"

So here are 2 git aliases publish and branch-name. Branch name alias I don't use just my itself but in other git commands. Git publish is one of them. Here we are without in origin with current branch name. Just to remind you git push without arguments will depend on the options and normally you will get an error that you need to specify a branch or track in what remote branch you need to push your local branch. With this command I don't need to do anything. It will just push in remote branch with the same name or created it if it's not there.

Now one more useful command is an alias

alias gnb='git nb' # new branch aka checkout -b

Normally we want to create a branch and switch to it directly. And you need to write git checkout -b and a branch name. It's too much to type and too difficult to remember. As you understand from the name it's git new branch.

Now about checkouting to branch. I have 2 aliases

alias gco='git checkout'
alias gcm='git checkout master'

So gco is just a short alias to switch a branch. Gcm is an alias to just to master branch because it is used to often and I don't want to type it again and again.

One more alias is for git add. Normally I just want to add all untracked files to git, make a commit and push it. This is my alias gaa just adds all files

alias gaa='git add .'

And my normal workflow is

gc "Some name"

Of course we also need to work with stash. As it's just an aliases I will simply show them to you.

alias gst='git stash'
alias gsp='git stash pop'

First of all we want to improve git log so that it looks better.

alias gl='git l'
l = log --graph --date=short

This will show short dates and most importantly graph representation of commits.

Now we are coming to the fun stuff.

recent-branches = !git for-each-ref --count=15 --sort=-committerdate refs/heads/ --format='%(refname:short)'

This command is really nice to see last 15 branches because when you switched branches several times you don't want to just search for the branch that you used 10 minutes ago by name.

Now some resets when something goes wrong.

uncommit = reset --soft HEAD^

git uncommit resets last commit and you get all files from commit in uncommited state. It's really nice when you made wrong commit.

One more ist snapshot alias

snapshot = !git stash save "snapshot: $(date)" && git stash apply "stash@{0}"
snapshots = !git stash list --grep snapshot

So if you are working and you didn't commited yet and you need to switch on other task you can for sure just git stash. But I normally do git snapshot. It stashes my code inside stash with the name of the branch and the date so it's much easier to find your stash later or to have several of them. Also I can always write git snapshots to see the list of my snapshots that I made.

I hope this videos brings you understanding how flexible and useful aliases in git are. But please remember than simply copying 50 aliases from my config or from config of other people won't do anything to use and you won't start to use them. It's better to understand each of them fully and apply in you git usage one by one until they will be in your muscle memory.

And actually a have a course about git where I'm teaching you everything that you need to know in everyday work with git. I will link it in the description below as well as my other courses.