100 Days of DevOps : Day 32

W

Wycliffe A. Onyango

Guest

Git Rebase: What It Is, Why It’s Useful, and How to Do It​


When collaborating on a project, developers often create branches to work on specific features while the main branch (master or main) continues to receive updates from other teammates. Eventually, the feature branch needs to be synchronized with the latest changes in the main branch.

There are two ways to achieve this: merging and rebasing.

What is Rebase?​


Rebase is a way of updating your feature branch with the latest changes from the main branch, but without creating an extra merge commit. Instead, Git takes your feature commits and β€œreplays” them on top of the current main branch.

Visualization:

Before rebase:


Code:
master:   A -- B -- C
feature:       \-- D -- E

After rebase:


Code:
master:   A -- B -- C
feature:              \-- D' -- E'

Here, D and E are your feature commits. After rebasing, they appear after C, as if your work had started from the newest version of master.

Why Use Rebase?​

  1. Cleaner history – avoids unnecessary merge commits, making the project timeline easier to read.
  2. Easier debugging – linear history helps when using tools like git log or git bisect.
  3. Keeps commits grouped – your feature work is neatly stacked on top of the latest main branch.
  4. Better collaboration – rebased branches are often simpler to review in code reviews.

⚠️ Note: Rebasing rewrites history. Do not rebase branches that are already shared with others unless your team agrees to it.

Rebasing Exercise​


Here’s how I did rebase on the repository: /usr/src/kodekloudrepos/beta.git:

Go to the repository:


Code:
   cd /usr/src/kodekloudrepos/beta.git

Fetch the latest updates from the remote:


Code:
   git fetch origin

Switch to the feature branch:


Code:
   git checkout feature

Rebase the feature branch with master:


Code:
   git rebase origin/master
  • If conflicts appear, fix them manually.

  • After fixing, run:

    Code:
     git add <file>
     git rebase --continue

Push the updated feature branch back to the remote:


Code:
   git push origin feature --force

Final Thoughts​


By rebasing, the feature branch was updated to include the latest changes from the master branch, without creating a merge commit. This keeps the history clean and focused on the actual work done in the feature.

Continue reading...
 


Join 𝕋𝕄𝕋 on Telegram
Channel PREVIEW:
Back
Top