Use GIT REBASE instead of GIT MERGE

In this quick post, I want to show how to use git rebase to keep your commit path clean and maintainable.

Use GIT REBASE instead of GIT MERGE
Use GIT REBASE instead of GIT MERGE

In this quick post, I want to show how to use git rebase to keep your commit path clean and maintainable.

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 changes before creating any pull request.

 Usually, there're two options for such cases:

  1. Using git merge
  2. 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 attempt. Imagine how messy the git log will be if we continuously use merging to keep our branch up-to-date. It also prevents tracking real commits since the majority of them will be created by merging.

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 log clean. It also keeps the commit log clean and readable. Assume that we want to rebase with the master branch then the usage of the command will like below:

git rebase master

If there will be any conflicts while rebasing, then you must resolve them, add changes and simply continue:

git add -u
git rebase --continue

In case you want to abort the process:

git rebase --abort

I prefer to avoid git merge at all except in PRs where it have to merge with master by creating a commit about it.

Support 🌏

If you feel like you unlocked new skills, please share them with your friends and subscribe to the youtube channel to not miss any valuable information.