Get more out of Git with Git aliases

| 5 min. (977 words)

One of my favorite things to do as a developer is sharing tips and tricks I have discovered that help me be more productive. Though most of us use Git on a daily basis, I’m surprised by how many developers don’t seem to take full advantage of all the great things you can do with this powerful tool.

One of my favorite productivity tips for Git is to use custom Git aliases.

So, let’s go over three of my favorite and most useful custom Git aliases.

1. Unstaging files

There are several aliases I want to cover, but Git unstage is easily my favorite. Let’s say you’ve just finished working on a moderately complex feature and you’re getting ready to commit your work. If you’re anything like me, you might try to commit early and often, but sometimes just get “in the zone” and end up making a lot of changes before remembering to make a commit. As a result you begin to stage your files into logical chunks, and realize you added a file you didn’t mean to.

The correct solution here is to use git reset, but maybe you don’t remember the exact syntax. After googling the proper command to do this, you may realize that the out-of-the box solution from git. git reset HEAD -- <file> seems a bit ambiguous. You might be thinking “Am I going to lose my changes?” and “ Is there a special flag I need to pass to make sure it doesn’t wipe out the work I’ve already done?" I know that reset doesn’t destroy my changes (because I’ve had to look it up so many times), but that isn’t very clear by just reading the commands listed from running git --help._

In my opinion, it would make more sense to have called the command git unstage, but I’m sure Linus had his reasons for naming it as he did 🙂. Luckily, Git makes it easy to create exactly that command by aliasing the built-in functionality using the following command:

$ git config --global alias.unstage 'reset HEAD --'

Setting this alias, now allows you to write $ git unstage <filename>

Syntactic sugar? Absolutely, but it now reads better, and you don’t have to worry about remembering the exact structure of the underlying command.

2. Stashing files

Here’s another common scenario.

Say you’re working on a ticket and realize that you can knock out a second related ticket at the same time, so you make those changes as well. Now it’s time to make your commits, but you want to push them up on different branches corresponding to each of the separate tickets. Though there are plenty of different strategies to handle this, let me describe my go-to method.

First, go ahead and stage the files that belong on your current branch. Next, you need to stash the rest of your changes so you can make a commit, push it, and then move to a new branch. One small problem though, if you just run git stash it will stash all of your work, including the staged, unstaged, and potentially untracked files. So off to Google you go, and find out that you can pass a couple of flags to handle them.

$ git stash -k -u

Just in case you are unfamiliar with those options, the -k flag tells git to keep your staged files staged, and the -u flag tells it to also stash any untracked files with changes. Of course, there is nothing wrong with just passing the appropriate flags and moving along, but chances are good you’ll have to look them up again the next time you face this situation.

Using the command I covered earlier, let’s make an alias that will be easier to remember and have the same functionality.

$ git config --global alias.stash-unstaged 'stash -k -u'

Now, using the command git stash-unstaged will keep your staged changes, allowing you to make your commit and push your changes. Next, create a new branch, and run git stash pop to get back all of your changes from the previous branch. Add. Commit. Push. Done!

3. Previous branch

The last alias I want to share is one I added to mimic the behavior of a shell command I like. I’ll often cd into a different project to reference a file there and then want to go back to the previous branch. In most UNIX shells, the command cd - will move you back to the previous directory.

This command was a HUGE time-saver for me as opposed to copying or remembering the full path to the previous directory, so I wanted to replicate it for use in Git. If your team is like mine, sometimes your branch names can be rather long and start with ticket numbers associated with the changes you are making. Rather than using the good ol’ fashioned git checkout <long-branch-name-with-ticket-number-and-short-description> you can introduce this alias: $ git config --global alias.col 'checkout @{-1}'

Now you can just run git col and you’re back on your previous branch! Easy-peasy, and no need to hunt down the exact name of your previous branch.

In conclusion

Hopefully, this article has helped you see how powerful Git can be, and how easy it is to personalize it to your specific needs. Though you may not use these exact aliases yourself, I hope that seeing how they are made and the concepts they encompass, will help you become more productive and confident in your Git workflow.

Further resources