4 ms·
Rebase also helps me make less useless commits. For example, if I commit something on a branch, and then later realize I made a typo in that commit, I can make
by shawnps 14y ago
Rebase also helps me make less useless commits.
For example, if I commit something on a branch, and then later realize I made a typo in that commit, I can make a commit that fixes the typo, then use git rebase -i and make it a fixup commit. Fixup squashes the commit into another one, and discards the message.
- chrismsnz 14y agoI use 'git commit --amend' which does basically the same thing. Both have to be done before pushing to remote, anyway.
- erso 14y agoThis works great but can only be used to amend HEAD. If you want to amend a commit that's HEAD~1 or further back, you need to either use the fixup strategy I mentioned above, or, if you really want to use --amend: * git rebase -i SHA-to-be-amended~1 * select SHA-to-be-amended for edit * make your changes * git commit --amend -CHEAD (This takes the message from HEAD without presenting you with the editor. If you wanted to edit the message, use -cHEAD.) * git rebase --continue It's good to know when and how to use both. The fixup strategy is much quicker and doesn't require you to do the rebase immediately. I will sometimes stack a few fixup commits and just do the interactive rebase (with autosquash) later.
- erso 14y agoI used to do the process you describe, but have greatly improved it using autosquash: * Add your changes to the index * git commit --fixup SHA, where SHA is the commit you want to fixup into * git rebase -i --autosquash, now or later When you commit with --fixup, Git will make a commit with a message that begins with fixup! and continues with the message of the SHA you gave it. When you enter the interactive rebase, Git will automatically have moved the fixup commit just below the commit it's going to be fixup'd into. With this method you don't have to manage moving around the fixup commits, or use a system to tell you where they need to go. I used to do messages like "git commit -m 'f into a98oe6e'", which works until you do a rebase and the SHAs change. You can also enable autosquash in your gitconfig (git config --global rebase.autosquash true) so you don't need to pass --autosquash when doing the interactive rebase.