Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

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 themerge 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 myrepo, a static site generator (SSG) writing with Python. It started with working on a new branchgit 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 classGenerator, and this class was called in themain() function. Therefore, I was trying to polish the code with variables rename, making code style consistent and rewriting the support ofconfig file functionality, which was wrote by other contributors.

The original support ofconfig file functionality was achieved by usingjson parser. However, there are several packages published to support the command line config file. Here, theclick_config_file module was used to support configuration file. In this way, themain() 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 toinp is not None and several indentations were fixed.

Finally, an exampleconfig file was added.

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

Eventually, merge to main use:

git checkout maingit merge refactoringgit push origin main
Enter fullscreen modeExit fullscreen mode

The combined commit can be foundhere.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

A raw watermelon.
  • Joined

More fromQiwen Yu

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp