DEV Community

Mingming Ma
Mingming Ma

Posted on

Refactoring and Git Rebase Experience

In this blog, I will share my experience about refactoring codes and git rebase process. Let's start!

Refactoring

Refactoring is a crucial aspect of software development, it can improve the maintainability, readability, and overall quality of our code. Usually we have the following approaches:

  • Eliminate the global variables.
  • Choose functions to compute values rather than storing them in variables.
  • Split the code into smaller parts
  • Enhance variable naming for clarity and readability.
  • Reduce redundancy by implementing shared functions or classes.

How I did in my project txt2html

I've identified several areas where refactoring is needed: I had a global variable related to my language attribute setting, logic lines were repeated for obtaining input file paths that only differed in extensions, and all functions were located in a single file. For now, I have removed the global variable, consolidated repeated lines, and improved variable naming for better readability. I plan to split the functions later.

I'd like to remind you that React has remained at version 18.2 for over one year. I assume they are busy with refactoring for the upcoming release. :)

Git Rebase in Refactoring

First, let's have a new branch for refactoring:

# Create a new topic branch off of main
git checkout -b refactoring main
Enter fullscreen mode Exit fullscreen mode

Then we do the changes and commit each changing. After we decide finish the refactoring, we need to squash all commits into one commit, here comes the git rebase:

git rebase main -i
Enter fullscreen mode Exit fullscreen mode

This command gives us all commits as follows:

pick <Commit Hash> <Oldest Commit Message> 
pick <Commit Hash> <Older Commit Message> 
pick <Commit Hash> <lasted Commit Message> 
...

# Rebase ...
#
# 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
...
Enter fullscreen mode Exit fullscreen mode

So we need to pick the oldest commits and squash all the other commits to that one, changes the above to:

pick <Commit Hash> <Oldest Commit Message> 
squash <Commit Hash> <Older Commit Message> 
squash <Commit Hash> <lasted Commit Message> 
...

# Rebase ...
#
# 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
...
Enter fullscreen mode Exit fullscreen mode

Then we save this and exit, git will do the left things for us.

We then need to use git commit --amend to modify the commit messages after the rebase step or git --amend --no-edit keep all commit messages. Last step is merge to main:

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

That's it! Hope it helps!

Top comments (0)