Git fu: Bash addons

My .bash_profile, sourcing git-completion.bash to get autocomplete for git itself and git-flow-completion.bash to get autocomplete for git-flow, along with two single character shortcuts for the most common shortcuts (s for `git status`, d for `git diff`). And my .gitconfig, among regular config and customizing colored output also defining a bunch of aliases:

[gist id=1287185]
(If you’re reading the feed, check this gist).

The export line configures bash to show the current directory (full path!), and, if in a repository, the repository type and current branch, looking something like this: ~/dev/qunit [git:master] $
For that to work, `vcprompt` needs to be installed. If you’re using homebrew:

`brew install vcprompt`

The last line in the .bash_profile overrides the `git` command to point at hub, which wraps `git` to provide a few additional commands that make it easier to work with GitHub projects. Among others, it infers projects based on the current git repository and its remotes, powering the git-fork alias, which I use whenever I test and merge pull requests. Say someone named `beefheart` sends a pull request for jquery/qunit. All I do to get his remote is this:

`~/dev/qunit [git:master] $ git fork beefheart`

So much easier then trying to put together the correct .git URL for his fork, then wrapping that with the right invocation of `git remote add -f`.

Another GitHub specific command is ghpages: It assumes you’re on master, pulls changes, checks out the gh-pages branch, merges master and pushes those changes, then checks out master again. Useful if you keep a 1:1 copy of your master branch as a gh-pages preview.

Other aliases I use all day:

  • `git cm “Message 123″` to commit all updated files without having to launch an editor
  • `git br` and `git co` to create, switch and delete branches
  • `git lp` to see commits with diffs
  • `git lg` to see a kind-of tree view of commits – will show the commits a merge commit added directly after that merge commit, instead of the usual chronological order of `git log`. Very useful to review a banch of merges.
  • `git undo` to revert bad merges
  • `git co .` to revert all changed files

Some more git related tools are provided by git-extras, where I now use git-changelog to create the release notes for QUnit.

I use iTerm 2 on OSX, if nothing else, for the ability to easily display multiple terminals side by side.

If you use ZSH, apparently you can get a lot of this bundled via oh-my-zsh.

-Jörn

No more comments.
  1. Nice.

    The annoying thing about oh-my-zsh is that the git-plugin prompt isn’t as awesome as the git-completion.bash.

    But there is a another neat feature in oh-my-zsh: the “mark git repo as dirty” thing when it is changed

    I managed to cram both features into oh-my-zsh like this:

    https://github.com/tfnico/oh-my-zsh/compare/master…tfnico

    My .zshrc is here: https://gitorious.org/testing-git/prefs/blobs/master/zshrc

  2. Not sure what “mark git repo as dirty” actually does, but seems like vcprompt also has that feature.

  3. Dave Methvin

    Thanks, Jörn! Very useful stuff.

  4. With dirty repo I mean changes in the working tree. Yes, I was missing this in the __git_ps1 function from git-completion.bash. Maybe I should try out vcprompt instead. I don’t know the features of vcprompt, but the __git_ps1 shows:

    * when you’re inside a bare repo
    * when you’re inside a .git folder
    * when you’re in the middle of a merge or rebase
    * when you’re in detached state, which rev you’re on

    I would kinda miss that if it’s not in vcprompt..

  5. Well, vcprompt shows you the repo type, the curent branch (including detached states) and an indicator of new or changed files.