Skip to content

Instantly share code, notes, and snippets.

@coderabbi
Last active January 28, 2021 10:54
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coderabbi/b0278212b2220fdf63746906fbb82770 to your computer and use it in GitHub Desktop.
Save coderabbi/b0278212b2220fdf63746906fbb82770 to your computer and use it in GitHub Desktop.
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