DEV Community

Qiwen Yu
Qiwen Yu

Posted on

Refactoring Code & Rewriting Git History

The easiest way to integrate the branches, as we’ve already covered, is the merge command. It performs a three-way merge between the two latest branch snapshots and the most recent common ancestor of the two , creating a new snapshot (and commit).

However, there is another way: you can take the patch of the change that was introduced on one branch and reapply it on top of another branch. In Git, this is called rebasing. With the rebase command, you can take all the changes that were committed on one branch and replay them on a different branch.

In the blog, I continued working on my repo, a static site generator (SSG) writing with Python. It started with working on a new branch git checkout -b refactoring, and the goal is to improve the structure and maintainability of the code without altering its behaviour. The main funtionality of this SSG has already been encapsulated in a class Generator, and this class was called in the main() function. Therefore, I was trying to polish the code with variables rename, making code style consistent and rewriting the support of config file functionality, which was wrote by other contributors.

The original support of config file functionality was achieved by using json parser. However, there are several packages published to support the command line config file. Here, the click_config_file module was used to support configuration file. In this way, the main() function was simlified a lot by replacing the json parser and reading json file line by line code part with a single function decorator (function annotation).

Moreover, the code style was improved on several places. For instance, inp != None was changed to inp is not None and several indentations were fixed.

Finally, an example config file was added.

Now, with several commits, we can use the terminal command git rebase main -i. This open an interactive window. Then, we can squash all commits into the first one. Additionaly, if there is a need, use git --amend to edit commit message.

Eventually, merge to main use:

git checkout main
git merge refactoring
git push origin main
Enter fullscreen mode Exit fullscreen mode

The combined commit can be found here.

Latest comments (0)