Git config and aliases to boost productivity
In Evionica we’ve implemented the git-flow with branch names including a Jira ticket identifier. Eg. feature/WBA-123-some-feature
. I am a fan of using git in the command line. But I found it pretty frustrating to type every time git checkout feature/WBA-123
before pressing the tab to autocomplete branch name. The only vital information is the ticket identifier. I’ve found out that it’s pretty easy to create git aliases to configure custom commands.
.gitconfig
It’s a small file containing some basic git configuration. On OSX it should be located in the user's main folder.
You can easily enhance your git experiences by editing this file. For example, you can change your basic text editor to nano, which I find much better than vim.
Aliases
Git aliases can be defined in the[alias]
section, and you may use inside them almost any command you want to.
Let’s consider that you want to jump to some branch, that you haven’t been working on recently, and there are some new commits. You need to git checkout branch-name
then git fetch
and git pull
. And that’s the most common simple scenario, that you probably follow every day. Let’s speed it up.
In my opinion, the most accessible way to define complex queries is by wrapping them into anonymous bash functions.
aliasName = "!f() { some commands; some more commands; }; f"
You can pass parameters to your aliases, and use them in the order by $1
, $2
etc.
Knowing that, let’s create simple alias, which we want to perform git checkout
of a given branch, and then git fetch
and git pull
. And we want to call it with git upd branch-name
command.
It’s pretty neat, huh?
More complex logic
I’ve mentioned that I wanted to switch to the branch by its ticket id. To achieve that I needed to create a similar alias. I’ve encountered several problems to solve. The very first thing to do is to find the branch name.
We will need to combine four commands. git branch --all
prints out a list of known branches in the current repository. grep -i -m 1 $1
will filter out the results to match searched value $1
, and return the first value 1
. echo $()
will print out the search result. If the first branch is remote,sed 's|remotes/origin/||g'
will remove the remote part, to prevent checking out the origin branch.
After finding the branch name, we need to check it out. Let’s wrap the finding command with $()
.
And now, instead of typing git checkout feature/WBA-123-...
, to checkout branch feature/WBA-123-planes-can-fly-upside-down
, we can just type git co WBA-123
, or even git co 123
if that number doesn’t occur anywhere else.
One more tool from my toolshed
I’ve configured a third alias, that you may find useful. Sometimes you need to perform a hard reset of your branch to the state of the remote one. I need to do so, after rebasing the branch directly in GitLab. After checking out the branch, the standard way to perform a hard reset includes two steps. First of all, you need to fetch remote branchesgit fetch
to ensure, that git will know the target state. Then, you can reset your current branch to the state of the remote one with git reset --hard origin/full-name-of-branch
.
With the below alias, you can perform a hard reset with git hr
.
Conclusion
There is a lot of ways to improve the quality of working with git in the command line using git aliases. I hope you found this tutorial helpful, and you already have some ideas for aliases to your common scenarios. Maybe you’ve even already configured some of them. Let me know in a comment.
Side note: if your command line doesn’t support git auto-completion, you may find the below extensions useful. Both of them are customizable and extendable and will may your work with git in the command line way smoother. Linux users usually have such features out-of-box.