The deep dive into the git universe continues. One really cool concept I have learned this week is git rebase: it is like moving a series of commits to a new base commit.
To practice, I worked on refactoring my static-dodo project in a new branch that I called
refactoring. I started by fixing the code format from previous contributions using prettier. I then changed the code to get rid of global variables. To do that, I had to split up some code into separate smaller functions. At each of these steps, I created a new commit to make sure I could easily go back to the previous version if anything broke (And things broke a lot while doing this!).
Once satisfied with what I had, I used the command
git rebase main -i to rebase into the main branch. The
-i stands for interactive mode and it opened my editor to allow me to customize my commit.
All the commits made in the refactoring branch are now displayed in chronological order. Each line shows a command word (
pick by default), a commit id and the commit message. Like this:
pick 28cd74de fix code format with prettier
These are the available commands to use:
# Commands: # 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.
squash for all the other commits in the list to merge them all into the first commit. To change the final commit message I used:
git commit --amend. Now the commit message includes everything. I could have also used changed
reword to edit the commit message during the interactive git rebase.
I believe the code is now easier to read and easier to maintain going forward. The code looks very different now and there is definitely more that could be improved. I love the git rebase workflow and it is for sure something I will continue to use going forward.