Rebase, Squash, Amend in Git
Tags: opensource
Published: 2021-10-14 21:09:09

# Project Introduction
This week, I work on my Static Site Generator (SSG) in C++ - [Potato Generator](https://github.com/kiennguyenchi/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](https://github.com/kiennguyenchi/potato-generator/commit/a0a986fd2be2c2d11f79dd725d880c4425e9b8a9)

# 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
```

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
```