DEV Community

Cover image for Use GIT REBASE instead of GIT MERGE
Rashid
Rashid

Posted on • Updated on • Originally published at thepylot.dev

Use GIT REBASE instead of GIT MERGE

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

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

In case you decided to abort the process:

git rebase --abort
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
zalithka profile image
Andre Greeff

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 of merge, is when the remote Git host protects your master, I mean main, I mean trunk, I mean source-of-all-truth branch... oh, and maybe the repeat notifications for those of us who mention issue IDs in commit comments. hmm..