Skip to content

Instantly share code, notes, and snippets.

@coderabbi
Last active January 28, 2021 10:54
Embed
What would you like to do?
Git Alias: fix

Ever wish you could add something to a prior commit, but you've committed a few times since?

Simple, right? git commit --fixup <hash-to-fix> followed by git rebase -i <last-good-hash>

I don't know about you, but that's one more hash than I want to deal with and since the --autostash option sets up the rebase properly for me, I really don't want to have to deal with my editor, either....

Consider the "git fix" alias.

Definition:

#.gitconfig

[alias]
    fix = "!f() {
        git commit --fixup $1;
        GIT_SEQUENCE_EDITOR=touch git rebase -i $1^ --autosquash --autostash;
    }; f"

Usage: git fix <hash-to-fix>

The alias handles everything for you from there, including the rebase, without any additional input.

If you're curious how the sausage is made...

  1. The alias defines a function...
  2. The first line of the function is the aforementioned commit with the --fixup option.
  3. The second line of the function temporarily sets the GIT_SEQUENCE_EDITOR to touch which means that instead of popping open your editor, the rebase file is marked dirty without any interactivity, meaning that the subsequent rebase -i (together with the --autosquash option) beginning with the commit immediately prior to the commit to be fixed ($1^) is going to happen for you automagically; the --autostash option allows you to return to work in progress immediately!

Now that is simple!

@RobinMalfait
Copy link

You forgot a little " there.

#.gitconfig

[alias]
    fix = "!f() {
        git commit --fixup $1;
        GIT_SEQUENCE_EDITOR=touch git rebase -i $1^^ --autosquash --autostash;
    }; f"

@coderabbi
Copy link
Author

Indeed. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment