How to Use Git Aliases to Increase Your Productivity

[ad_1]

Git is a really highly effective device, and it may be slightly scary typically. It would not matter how lengthy you have used it, you’ll find your self trying to find “how to do X”.

Today I need to discuss aliases. Aliases are quick, customized made instructions that translate into different instructions. Aside from saving keystrokes, aliases allow you to avoiding memorizing command choices or digging in your shell historical past to discover the command you want.

There are 2 forms of aliases: Git aliases and shell aliases, managed by bash, zsh, and so forth. Let’s begin with Git aliases.

How to create Git aliases

Git aliases will be saved globally or in particular person repositories. Unless you’ve a really particular purpose to maintain an alias scoped to a mission, I’d suggest modifying the worldwide config file, which is positioned at ~/.gitconfig.

Aliases dwell within the part [alias]. They can be found in your terminal as Git choices the identical approach add, commit and different choices work. Let’s see how to add an alias, step-by-step:

First, open your Git config file. In my case, I’ll use VS Code:

code ~/.gitconfig

Next, let’s add our first Git alias:

[alias]
	graph = log --oneline --graph --decorate

After saving the file, go to an current Git mission and execute git graph. The result’s related to the picture beneath:

git graph example in the Tailwind CSS repository

Instead of typing git log --oneline --graph --decorate and remembering all three flags, now you should utilize git graph and get the identical end result.

My favorite Git aliases

Here are some aliases that I’ve been utilizing quite a bit currently:

[alias]
	graph = log --oneline --graph --decorate
	ls = log --pretty=format:"%C(yellow)%h%Cred%d %Creset%s%Cblue [%cn]" --decorate
	ll = log --pretty=format:"%C(yellow)%h%Cred%d %Creset%s%Cblue [%cn]" --decorate --numstat
	lds = log --pretty=format:"%C(yellow)%h %ad%Cred%d %Creset%s%Cblue [%cn]" --decorate --date=quick
	conflicts = diff --name-only --diff-filter=U
	local-branches = !git department -vv | reduce -c 3- | awk '$3 !~/[/ { print $1 }'
	recent-branches = !git branch --sort=-committerdate | head
	authors = !git log --format='%aN <%aE>' | grep -v 'users.noreply.github.com' | sort -u --ignore-case

graph, ls, ll and lds are useful for history visualization. I use git ll a lot since it gives me the commit, the changed files, and how many lines were modified, as shown below:

git ll example in the Tailwind CSS repository

conflicts returns a list of conflicts which you can just copy and paste in your editor.

local-branches returns a list of branches available locally and not in origin (GitHub for example). This can be useful if, for example, you need to double-check if your team has access to your branches.

recent-branches is probably the one I use the most in this list. If you are working in different features at the same time and jumping from branch to branch, this is a must-have.

authors is handy for open-source work. You can run git authors > AUTHORS.txt and see a list of people who have worked in the repository.

Shell aliases with Oh My Zsh Git plugin

The list above is quite small, you may think. And it is indeed. This is because I also use the aliases created by the Oh My Zsh Git plugin.

Oh My Zsh is a framework for managing your zsh configuration. With this tool, you can customize the shell prompt, use different themes, and add plugins that offer aliases to increase your productivity.

Shell aliases are created in a slightly different way. They live inside the shell config files (usually ~/.bashrc or ~/.zshrc, if you use Bash and Zsh respectively).

Here is an example created by the plugin:

alias gst='git status'

If you don’t use Oh My Zsh, you can still copy the line above and paste it in the end of the ~/.bashrc or ~/.zshrc files – the shell alias will work fine.

Instead of setting up a Git alias st for status, I use the shell alias gst which gives me the same result.

I prefer shell aliases over Git aliases for 2 reasons:

  1. Shell aliases don’t need to be prefixed by the command git.
  2. I don’t need to maintain a list of aliases since I can just learn the ones maintained by the Oh My Zsh community.

Protip: If you are using the default shell without any customization, I do recommend checking out the Oh My Zsh project and Wes BosCommand Line Power User.

Conclusion

Aliases are shortcuts you can create or use to increase your productivity. Today I showed two ways to use them in Git.

I hope this post inspires you to re-imagine how you use the terminal. What about starting to create your own aliases?

You can read more articles like this on my blog. If you like this content, you can follow me on Twitter and GitHub.

Cover photo by Yancy Min/Unsplash



[ad_2]

Source hyperlink

Write a comment