Some Useful Git Aliases

I started using Git years ago, almost always from the command-line. There are great GUIs available, as well as plugins for many editors, which provide user-friendly access to most of the Git commands you will ever care to use. But some people, like myself, are in the habit of using command-line interfaces. And years of using Git from a terminal results in the creation of an alias or two for brevity and utility.

These are ones I use most often.

git reword-last

Definition: reword-last = commit --amend -C HEAD

I often notice typos or other mistakes in commit messages after I finish writing them. For instance, I routinely forget to mention some important detail about the patch. This git reword-last alias opens the latest commit in my editor so that I can fix its commit message, amending the existing commit.

git whatsup

Definition: whatsup = log --all --decorate --oneline --graph

I use this alias to refresh my memory about what’s going on in a repository before I set out coding. It is a poor man’s terminal version of output from a GUI. For example, the output of git whatsup looks like:

* c8d5586 Improve the clarity in the instruction documentation about opcodes
*   b1f9c0d Merge branch 'additional-positions'
|\
| * 342d5ca Fix two table accesses in Drawable:setPosition()
| * 1cbc35a Set the position for Drawables inside the ‘draw-image’ instruction
| * d50a87b Document the ‘position’ argument for the ‘draw-image’ instruction
| * e66ca93 Add comments explaining each step of the module loading process
| * fad116e Correct the documentation regarding position values in the settings
| * 0af2601 Alter the comments for the ‘set-scene-image’ opcode for clarity
| * 5edeb3b Fix typos in the comments for the LNVL.Opcode.Processor table
| * 66d678b Make the example array more clear in the ClampedArray module comment
| * d3a502b Add a script for testing out all of the position values
| * 7d62724 Fix a crash when scenes have transparent dialog boxes
| * c2a706f Teach Drawable:setPosition() to deal with all possible positions
| * 582e84c Document the new position values in the Howto
| * a7f4e3f Show the ‘position’ of a Drawable when converting it to a string
| * e3cf819 Add the diagram of positions from the previous commit message
| * b2566e0 Define LNVL.Position values for nine parts on the screen
| * ae4eaa2 Dynamically define the values for LNVL.Position
* |   e8b5763 Merge branch 'character-specific-fonts'
|\ \
| * | 31787fe Teach the ‘say’ instruction how to handle character fonts
| * | fa8a886 Teach Scene:drawText() to optionally accept a font as an argument
| * | bea04c9 Correct the comment for Scene:drawText() regarding the container
| * | 18ebe58 Create TAGS entries for code implementing opcodes and instructions
| * | 93e745f Allow the ‘font’ property when constructing Character objects
| * | 17a3015 Document the use of character fonts in the Howto
|/ /
* | dcda4d0 Add comments explaining each step of the module loading process
* | 0bca5b9 Correct the documentation regarding position values in the settings
* | c5c85bc Alter the comments for the ‘set-scene-image’ opcode for clarity

Restricted For Time

Often I only care to see the output of git whatsup for changes from the previous day, or since last week. The git log command, which the alias uses, accepts a --since parameter that can restrict the output to only those commits fitting into the given time-frame. So I frequently use these two aliases, built on top of the previous:

  1. today = !git whatsup --since="yesterday"
  2. week = !git whatsup --since="last week"

Now I can use git today and git week to only see current changes.

git aucv

Definition: aucv = !git add -u :/ && git commit -v

The awful four-letter name comes from the commands and parameters in the alias:

  1. add
  2. -u
  3. commit
  4. -v

It looks like I’m trying to randomly pick names from the periodic table of elements—terrible, I know.

This is the alias I use the most. It adds and commits changes at the same time. However, it only adds changes in files already tracked by Git (-u), and only those which are in the current directory or any of its sub-directories (:/). I prefer this approach instead of using something like git add -a or git add . because it decreases the chances that I accidentally commit something I did not mean to commit.

git master-merge

I need to explain my common workflow before this one. By default Git calls the first branch the master branch, and I rarely change that. When I am creating new features or fixing bugs I always start out doing that on a new branch. When finished I merge that branch back into master. Except—to throw out some Git terminology—I avoid ‘fast-forward merges’. If my new branch contains only a single commit then I will use a fast-forward merge, which is Git’s default behavior, but that is the only time. In any other situation I want to explicitly not perform a fast-forward merge. There are articles that explain my reasoning behind that preference.

Now for the alias:

Definition: master-merge = !git checkout master && git merge --edit --no-ff @{-1} && git branch -d @{-1}

You’ll see three commands in there. Because when I run git master-merge from one of my branches it performs the following:

  1. Switches back to the master branch.

  2. Merges the branch I was just on (@{-1} is the short-hand for that) into master, opening the merge commit in my text editor so I can make sure it looks right first. And note this does not perform a fast-forward merge.

  3. Delete that branch I was on if there was no error in the merge process. If the merge failed for any reason then the branch remains. This provides a short-cut for stopping the merge if something looks wrong: save an empty commit message in my editor. Git will intentionally not accept an empty commit message as a way to stop an in-process git commit, git merge, et cetera.

And There Are Others

I use more aliases than that on a regular basis, but the rest rely on external scripts that are not part of Git, utilities my friends and I put together over the years. Someday I will get around to making those scripts available from my GitHub page. Someday….

If you have any Git aliases you commonly use then please share, and enjoy!

Advertisements

Add Your Thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s