DEV Community

Thanh Van
Thanh Van

Posted on

Refactoring my code - Git Rebase

This week I will be working on refactoring my code to make it look nicer, shorter, and easy to maintain my source code later on. Moreover, I have a chance to practice on new knowledge such as git rebase, git commit --amend, and definitely git merge.

HOW and WHERE to start?

The first thing I need to do is make sure I update everything from my Github into my local machine. Then I create another branch named refactoring, and I will do my changes in this branch, which will not affect any things in my original code.

I have to read my code again and again to find where needs to be fixed. In this process, I will mainly focus on changing variables and functions name, and also getting rid of Global Variable. I do not make a lot of changes in my code, so it still works the way it does. When I first write my code, I already want it to be short and easy to maintain, so I do not have to do much changes now.

After I get everything done, I do an Interactive Git Rebase in order to squash all of my factoring commits into a single one. Here is the moment I encounter a problem while trying to squash my commits. I did not notice that I would be in a different edit mode when doing the interactive git rebase, so when I tried to change the word pick into squash for my last commit, I pressed the delete/insert button and it deleted the name for the commit. It was b2f4c75 and I somehow pressed it into 4c75, I did not remember its name and I either did not know how to undo it or exiting it and tried the new one. After an hour later, I figured out there was a little note at the bottom left to notice me which mode I was in. I pressed i to get into insert mode, and changed my commit, then exit by pressing Esc and type :x!. After squashing my commit into a single one, I used Amended Git Commit to update my commit message.

Once again, Git is so powerful, it allows me to change my code's history, and make it look more simple. Later one, I would easily catch up what I have done for each commit I have made.

Top comments (0)