Git – Bash Command-line Aliases

Just as at many other companies, at Monetate we use Github as our source control system. Recently I decided to dedicate some time to create some helpful aliases to save some time. Assuming that you create new branches all based off of your “master” branch these aliases may be helpful for you as well:

[alias]
	# Show a log of all commits with any pertinent files listed below each
	# commit.
	lo = "log --name-only --color --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

	# Creates a new branch and checks it out.
	b-co = checkout -b

	# Remove the specified local branch.
	b-rm = branch --delete

	# Remove the specified remote branch.
	rb-rm = push --delete origin

	# A diff showing what has been added or taken away in this branch in
	# comparison to what is in master.
	b-diff = "! git diff 'HEAD~'$(git log HEAD ^master --oneline | wc -l | tr -d ' ') --color"

	# Show a log of all commits found in this branch but not in master.
	b-log = log master..HEAD --color --name-only

	# Rebase this branch, allowing for all commits added in this branch to be
	# modified.
	b-rebase = "! git rebase -i 'HEAD~'$(git log HEAD ^master --oneline | wc -l | tr -d ' ')"

	# Opens a new window in Sublime Text 2 with all the files that have been
	# modified in this branch.
	b-subl = "! cd $(git rev-parse --show-toplevel) && /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl --new-window $(git b-diff --name-status | awk '/^[^D][ \t]+(.+)/ {print $2}')"

All of the above aliases can be added to your .gitconfig file which is probably located at “~/.gitconfig“. The following explains what adding each of the above aliases actually does:

Command Description
git lo Similar to a git log but instead of printing each commit in its own block, each commit only shows on one line: efce549 - Name of the Commit (28 hours ago) <John Doe>
git b-co <new_branch_name> Creates the branch specified and then checks it out.
git b-rm <branch_name> Removes the specified local branch. In order to force the removal of the branch you can use git b-rm -f <branch_name>.
git rb-rm <remote_branch_name> Removes the specified remote branch. In order to force the removal of the remote branch you can use git rb-rm -f <branch_name>.
git b-diff Does a git diff HEAD~# where # is the number of commits found in this branch which are not found in your local master.
git b-log Shows the logs for all of the commits found in this branch which aren’t in the master branch.
git b-rebase Does a git rebase -i HEAD~# where # is the number of commits found in this branch which are not found in your local master. Especially useful for squashing all of your commits into one.
git b-subl Opens all of the files which have been updated in this branch in Sublime Text 2 (in comparison with what is in your local master). NOTE: This may need to be modified if Sublime Text 2 is not located at /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl.

It is important to note that you can change the “Sublime Text 2″ shortcut to the editor of your choice. I just put that there to show you how to get all files that were updated in this branch to open. Have fun, and let us know if you have any modifications or additional aliases! 8)

Leave a Reply

Your email address will not be published. Required fields are marked *


three + 6 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="">