DEV Community

Kien Nguyen Chi
Kien Nguyen Chi

Posted on

Rebase, Squash, Amend in Git

Project Introduction

This week, I work on my Static Site Generator (SSG) in C++ - Potato Generator. After weeks that people worked on and fixed my code. Finally, I have time to revise everything, make my first commit with Git Rebase, Squash and Amend

Git Rebase, Squash and Amend

๐Ÿ‘‰This week, I am newly introduced about this concept of Git. This concept is really helpful to fix these issues that I have before.

โ“Sometimes, for example, I put a line of code to print a message in my program help menu. I commit it with message "update help menu". Then I realize that I would need to add more lines to the help menu at some points in the future. So what the commit messages would be like? "update help menu 1", "update help menu 2", ... so on. I would not make sense at all and I would create a tons of duplicated commits, which is not necessary at all.

โœ”๏ธNow, I have a Git tool to work with it. Every time I face that problem again. I just have to do git rebase git rebase main -i to open an editor, pick whatever commit/commit messages I want to combine into, then squash the others commit I want to combine. I can go on with git commit --amend to edit the entire commit messages, create them with new name and in nice format.

Apply Git into my Project

In total, after reviewing over my code, I create 5 commits, which are:
๐Ÿ‘‰ Restructure the code in main function: After seeing how the code is going on, I decide to make a change in if/else structure in my main function in pgprogram.cpp in order to reduce the amount of code and make them logical to the reader.
๐Ÿ‘‰ implement get ouput/lang functions: I created 2 separate functions to get inputs of language and output files from command line arguments. Before, I put all these code into main, which makes it so big. Breaking down the code is necessary to do.
๐Ÿ‘‰ improve variable/function naming: I change some name of the functions and also variables so that the readers can understand them easily. For examples, I change function createOneHTML() to createHTMLFile(), createManyHTML() to createMainPageWithHTMLs() or variable fname to fileName.
๐Ÿ‘‰ reorganize files in folder and add .gitignore: I reorganize my folder by removing unnecessary files and create a .gitignore for untracked files.

I use git rebase, pick the commit Restructure the code in main function and squash the remaining 4 into a single commit message to produce this result:

    Restructure the code in main function

    implement get ouput/lang functions

    improve variable/function naming

    add .gitignore

    reorganize files in folder
Enter fullscreen mode Exit fullscreen mode

Then, I use git commit --amend to edit the long, unorganized commit messages, create a new name and put them in nice format. The final commit message result would be like:

commit a0a986fd2be2c2d11f79dd725d880c4425e9b8a9 (HEAD -> main, origin/main, origin/HEAD, refactoring)
Author: Kien <kiennguyenchi11@gmail.com>
Date:   Tue Oct 12 17:09:37 2021 -0400

    Refactoring files in the folder to improve code maintainability:
    * Restructure the code in main function
    * implement get ouput/lang functions
    * improve variable/function naming
    * add .gitignore
    * reorganize files in folder
Enter fullscreen mode Exit fullscreen mode

Top comments (0)