Show HN: 'git inject' – amend commits other than HEAD

https://news.ycombinator.com/item?id=9705690
by koreno • 11 years ago
71 36 11 years ago

'git inject' is a git alias (see code at the bottom). It is similar to 'git commit --amend', but it allows you to 'inject' your changes into commits further back in history (using 'rebase').

If you're as pedantic as I am about the git history you're about to push into master (as far as I can control it, I strive to keep each commit conceptually coherent), you'll often come to a situation where you have a modification that really belongs in an older commit. It takes a few git commands to stash other changes away, commit the modification, then interactively rebase over some previous commit, move your commit into place, change to 'fixup', save, exit, and then unstash whatever else you had lying around. Not fun.

Here's how 'git inject' makes it fun:

> git inject <commit-ref> <patch-ref>

> git inject HEAD^^ -a # inject all work-dir modifications

> git inject a28kd8 -p # interactively select patches to inject

> git inject HEAD~4 file1 # inject the modifications in file1

Just put this into your .gitconfig under 'aliases':

inject = "!f() { set -e; HASH=`git show $1 --pretty=format:\"%H\" -q`; shift; git commit -m \"fixup! $HASH\" $*; [ -n \"$(git diff-files)\" ] && git stash && DIRTY=1; git rebase $HASH^^ -i --autosquash; [ -n \"$DIRTY\" ] && git stash pop;}; f"

Related Stories

Loading related stories...

Source preview

news.ycombinator.com