TIL: use git interactive rebase to combine multiple commits in different ways
2022-06-04 00:00:00 +0000 UTCBy running the command git rebase -i
you can combine multiple commits into one or more commits. It also allows rewriting commit messages and dropping commits. The feature works by providing a simple script that has one line for each commit with three columns: a command, a commit ID, and the commit message from that commit. To determine how the commits will be combined, modified, or discarded you only need to edit the command to the left of the relevant commit. An example:
# A list of commits are included here
pick abcdef One commit
reword fedcba Another commit
squash efdbac A third commit
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c <commit> to reword the commit message.
So when finishing this rebase process, two commits will be generated: the first commit alone with the commit message “One commit,” a second commit with ID fedcba
and a rewritten commit message and the content of the third commit. Once the rebase is finished, you are presented with the final commit message for the new resulting commit or commits in your default editor.
More information about git rebase
in general available here.