A Blog Less Ordinary

The blog of Dave Ingram

Useful git configuration fragments

Time for a quick micro-post, I think. I feel like sharing two useful fragments of my git configuration.

First up, aliases. I have a few useful shortcuts defined, as well as some nice ways of displaying a repository’s history. I’ve aliased many of the common commands to two-letter versions which evoke the similar Subversion command aliases:

† st = status
  di = diff
  co = checkout
  ci = commit
  br = branch
  sta = stash
† graph = log --decorate --oneline --graph --branches --date-order
† lg = log --graph --pretty=format:'%C(yellow)%h%Creset -%C(yellow)%d%Creset %s %C(green)(%cr)%Creset %Cred<%an>%Creset' --abbrev-commit --date=relative --date-order
  today = !git log --author=$(git config user.email) --since=yesterday

The last alias (git today) reminds you what you’ve done today. The previous two aliases provide a nice graphical view of history in the console. git graph displays all history of all local branches in date order rather than topological order. This means that commits on different branches may be interleaved, rather than grouping commits on different branches. git lg displays a little more information (including author and relative timestamps), and allows me to pick which branches I want to see. The examples below are all taken from the Jerity repository.

The second tip is repository URL rewriting. This tackles the annoying problem of having submodules in projects that you want to be able to commit to, while making sure other people only see/use the read-only URLs. Git will very helpfully rewrite URLs from one form to another, so by having:

[url "ssh://git@github.com/dingram/"]
† pushInsteadOf = git://github.com/dingram/

in my configuration, I can git clone git://github.com/dingram/repository and when I push, it will automatically push to git@github.com:dingram/repository instead. This can be verified using git remote -v. The only thing to beware of is the format: the replacement URL goes in the section header, and the pattern to be replaced appears in the section itself.

Leave a Reply

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




GitHub Google+ Twitter