DEV Community

Andy Yang
Andy Yang

Posted on

Refactoring code - git rebase

Alt Text

To practice git rebase and Refactoring my code, I worked on my goURL repo.

I planned to do three commits with three different refactors.

  • extract functions to reuse.
  • improve variable names.
  • split the util functions to a new file utils.go

Extract functions and improving variables go smoothly. However, when I split the util functions, all my code is broken. all my imported library is still on main.go,
I checked all the libraries needed by the new file and deleted the old imports from main.go. Then, everything works.

The process of refactoring:

git checkout -b refactoring master
// refactoring 1
git add .
git commit -m "Extract functions to reuse"

// refactoring 2
git add .
git commit -m "Improve variable names"

// refactoring 3
git add .
git commit -m "split the util functions to a new file utils.go"

// git log to check the commit history
git rebase master -i
// change commits message.
git commit --amend


// merging
git checkout master
git merge refactoring
git push origin master
Enter fullscreen mode Exit fullscreen mode

git rebase is a way to clean our commit history. we also use git rebase to
update our working branch to fetch the latest commit from the remote repo.

Oldest comments (1)

Collapse
 
andreacanton profile image
Andrea Canton

I think rebase is good when you need to get everything in one timeline. Is good to rebase when you pull from a remote branch so you don't create a merge commit with the same branch. But rebasing before merging other local branches is not a good idea, you loose the timeline of when you made the edit.