Git Rebase Interactive: Change the past

Rebase is one of the most often used commands, as a way to update your tree with all the changes that happened in master since you branched. But when used with its --interactive (-i) flag it turns into the most powerful history changing git command. If you want to simplify your commit structure before submitting your code, if you are maintaining a public repository or if you messed up with your commits git rebase --interactive is a great option. When you run it with HEAD~5 it will show you your last 5 commits prepended by the word pick.

pick r extract strings to constants file
pick F add early exit for invalid neighbours
pick a fix linting issues
pick e add eslint/no-yoda rule

You can then replace this prefix with different options, for the complete list check the docs. Here we’ll center on reword and edit. Reword is the equivalent to commit --amend --message "new message", but allows you to batch edit several commit messages. Edit is the most powerful option and allows you to amend past commits. After you accept the prompt it will open your editor in the state of the edit prefixed commit. Then you can modify your code, stage your changes and commit --amend them. Next, you git rebase --continue and your history will now contain your newly committed changes, with the message you chose.

Use this aliases (already included in the Oh My ZSH git plugin) alias grbi = git rebase --interactive

This entry is part of the Useful Git Aliases series.