Aug 15

Despite being a blasphemous Windows user, I still  love using the git bash. It's way quicker than GUI for many operations, and it allows me to immediately check which branch I'm on. Combined with the knowledge of a few keyboard shortcuts, I can also perform quick operations on multiple repositories in sequence by having several bash instances open. This flexibility is simply unrivaled by GUI.

There are some operations, though, where the power of GUI really shines through. When committing changes, for example, I like to go over every file I changed and  go over the changes quickly to make sure there are no stray debugging leftovers, surprise code changes that I didn't expect, or commented out sections that need to be uncommented. I find this habit to be a valuable tool in reducing bugs. Via Bash, it is much harder to quickly scan large amounts of code. Another example where GUI is better is when resolving merge conflicts. Again, the ability to see large amounts of code, in this case side by side, makes Bash the lesser tool when compared to GUI. Finally, viewing the changes log is something I do often; GUIs tend to have log features that the default git log command doesn't support: A graph showing how the branches are connected, changed files for each commit, etc.

Therefore, I came up with a few shortcuts that trigger the GUI from Bash, winning me both the flexibility of command line and the ease of use of large graphical screens when I need them. In this tip I will be demonstrating with the TortoiseGit GUI, but the same principle can apply to any GUI that offers command line arguments.

Triggering TorgoiseGit from Bash

Msysgit on windows provides you with a bash environment that has many of the features you'd expect on a linux machine. One of them is the ability to configure aliases - commands that execute other commands. We're going to configure a few aliases the open tortoisegit from Bash for us.

Add tortoisegit bin folder to your environment Variables

We need to execute tortoisegit from bash, so we want its executables available from wherever we are.

Click the start menu, and start typing "Environment". The option Edit System Environment Variables  should appear. Click it (You can also use the user variables if you're using a shared machine).

Select the PATH variable, and add the location of your tortoisegit installation, plus the bin folder. Here is how mine looks like:

Editing your PATH environment variable


Click OK.

Add Aliases in Bash

Open your git bash, and type cd ~ [enter]

Go to home directory in bash

We are now at the home directory of your user. This is the directory that should contain the file that defines our custom bash aliases. This file is .bashrc

You can create it or edit it with notepad, but since this post is all about bash, I'll be adding the aliases using the command line.

First shortcut to add is log. I call mine "tlog", where t signifies that it's using tortoisegit

Type the following in your bash and click enter

 echo "alias tlog='tortoisegitproc /command:log'" >> .bashrc


Add a bash alias

Note: By using the >>, we're appending to .bashrc (or creating it if it wasn't there yet).

Test the shortcut out by closing your bash window, reopening it, and navigating to a git repository. Type tlog and press enter. If all goes well, you should see tortoisegit's log window open for the directory you're at.

Test your bash alias out


Let's add the rest of the commands:

tcommit: commit changes

 echo "alias tcommit='tortoisegitproc /command:commit'" >> .bashrc

tresolve: resolve conflicts in current merge

 echo "alias tresolve='tortoisegitproc /command:resolve'" >> .bashrc

While you're at it, consider adding some aliases for long commands you find yourself writing often. For example, I like adding shortcuts to go to my repository root directories:

 alias cdbooks='cd /f/myrepos/dualbook-creator'

Other commands I've added aliases to are pushing and pulling & viewing branches sorted by creation date. I'm sure you have your own use cases that could use an alias. Just pay attention to what long commands or operations you routinely do, and shortcut it with the above technique. For example, you could combine going to a repo and pulling the latest changes by chaining a change directory command and a git pull/fetch command. Just an idea, you get the gist of it.

Enjoy your increased hacking speed 🙂

Leave a Reply