A collection of git tips

Fri 19 February 2016 by Lars Kellogg-Stedman Tags git

This is a small collection of simple git tips and tricks I use to make my life easier.

Quickly amend an existing commit with new files

I have this alias in place that will amend the current commit while automatically re-using the existing commit message:

alias.fix=commit --amend -C HEAD

With this in place, fixing a review becomes:

$ vim some/file/somewhere
$ git add -u
$ git fix

Which I find much more convenient than git commit --amend, following by saving the commit message.

What files have changed?

Sometimes, I just want to know what files were changed in a commit. I have the following alias, because I have a hard time remembering whether I want --name-only or --names-only or --only-names...

alias.changed=show --name-only

Which gets me the commit message and a list of changed files:

$ git changed
commit 8c2a00809817a047bf312b72f390b5cb50ef9819
Author: Lars Kellogg-Stedman <lars@redhat.com>
Date:   Wed Feb 17 11:11:02 2016 -0500

    yet another attempt at fixing image fetching

    this uses curl rather than wget, because wget chokes on file:// urls
    which makes it difficult to cache images locally.  curl supports
    resuming downloads, but explicitly rather than implicitly like wget,
    so we need a do/until loop.

    Change-Id: Ibd3c524ea6ddfd423aec439f9eb7fffa62dfe818

:100644 100644 342a002... 2b9f9cb... M  playbooks/roles/libvirt/setup/undercloud/tasks/main.yml
:100644 000000 d22cb99... 0000000... D  playbooks/roles/libvirt/setup/undercloud/templates/get-undercloud.sh.j2

Getting the name of the current branch

For scripting purposes I often want the name of the current branch. Rather than reading git rev-parse --help every time, I have this alias:

alias.branch-name=rev-parse --abbrev-ref --symbolic-full-name HEAD

Which gets me:

$ git branch-name
$ git checkout bug/missing-become
$ git branch-name

Prevent accidental commits on master

When working on upstream projects I always want to be working on a feature branch. To prevent accidental commits on master I drop the following script into .git/hooks/pre-commit:


current_branch=$(git branch-name)

if [ "$current_branch" = "master" ]; then
  exit 1

exit 0

If I try to commit to my local master branch, I get:

$ git ci -m 'made a nifty change'

Automatic git cache

Mon 19 October 2015 by Lars Kellogg-Stedman Tags git

This post is in response to a comment someone made on irc earlier today:

[I] would really like a git lookaside cache which operated on an upstream repo, but pulled objects locally when they're available

In this post I present a proof-of-concept solution to this request. Please note that thisand ...

read more

Using tools badly: time shifting git commits with Workinghours

Fri 10 April 2015 by Lars Kellogg-Stedman Tags git hack

This is a terrible hack. If you are easily offended by bad ideas implemented poorly, move along!

You are working on a wonderful open source project...but you are not supposed to be working on that project! You're supposed to be doing your real work! Unfortunately, your extra-curricular activity is ...

read more

Tracking down a kernel bug with git bisect

Mon 21 July 2014 by Lars Kellogg-Stedman Tags docker kernel git

After a recent upgrade of my Fedora 20 system to kernel 3.15.mumble, I started running into a problem (BZ 1121345) with my Docker containers. Operations such as su or runuser would fail with the singularly unhelpful System error message:

$ docker run -ti fedora /bin/bash
bash-4.2# su ...
read more

Automatic maintenance of tag feeds

Fri 22 November 2013 by Lars Kellogg-Stedman Tags blog git

I recently added some scripts to automatically generate tag feeds for my blog when pushing new content. I'm using GitHub Pages to publish everything, so it seemed easiest to make tag generation part of a pre-push hook (new in Git 1.8.2). This hook is run automatically as part of the git push operation, so it's the perfect place to insert generated content that must be kept in sync with posts on the blog.

read more