This post cross-published with OnePublish
I want to show how to use git rebase to keep your commit path clean and maintainable.
What is git rebase
?
It behaves like merging by applying all changes from the target branch but not creating an extra commit for that where it keeps the commit log clean and readable. Simply, it takes your new commits at puts them at the very top of commit log.
Let's assume that you're implementing a new feature by creating a new branch from the master. Meanwhile, you're co-workers also working on other features and some of them already merged their PRs to master branch.
At this point, your branch will be no longer up-to-date with master so you need to apply those changes before attempting to create any pull request.
Usually, there're two options for such cases:
Using git merge
Using git rebase
Let's say you decide to use git merge
to apply changes from master
where it will create an extra commit for each merging. Imagine how messy the git log will be if we continuously use merging to keep our branch up-to-date. It also makes hard to track real commits since the majority of them will be created automatically by merging.
Example scenario
Assume that we want to rebase with the master
branch to apply most recent changes:
git rebase master
If there will be any conflicts while rebasing, then you have to resolve them and also adding changes once you finished:
git add -u
git rebase --continue
In case you decided to abort the process:
git rebase --abort
Once rebasing finished, you will able to see new changes in your branch.
I prefer to avoid git merge at all except in PRs where it have to merge with master by creating a commit about it. So, when analyzing git log it will be much easier to understand purpose of commits.
Video Explanation
Top comments (1)
I guess some people like changing history, and others don't.. lol. this one (looks at article), apparently has no problems with that.
TBH, the only "real" issue I can see with using
rebase
in place ofmerge
, is when the remote Git host protects yourmaster
, I meanmain
, I meantrunk
, I meansource-of-all-truth
branch... oh, and maybe the repeat notifications for those of us who mention issue IDs in commit comments. hmm..