Git Rebasing: A Practical Guide (with Examples)

Updated: January 28, 2024 By: Guest Contributor Post a comment

Introduction

Understanding the intricacies of version control is pivotal for streamlining development workflows. Git, as a distributed version control system, provides a plethora of powerful tools, one being rebasing. This guide is intended to offer practical insights into Git rebasing along with coherent examples to help improve your code management skills.

Why Rebase?

Rebasing is a Git process that integrates changes from one branch into another. Unlike merging, which creates a merge commit in the process, rebasing rewrites the project history by applying commits from one branch onto another, resulting in a linear project history.

Rebasing can make your project’s history cleaner and simpler. It’s often used before merging a feature branch into the main branch to simplify potential conflicts and to tidy up a series of otherwise messy commits.

How to Perform a Rebase

Let’s go through the basic steps to perform a rebase.

Step 1: Update the Base Branch

git checkout main
 git pull origin main

It’s essential to ensure that the branch you’re rebasing onto is updated to the latest version.

Step 2: Start the Rebase

git checkout feature-branch
 git rebase main

This command sequence rebases the feature branch onto the main branch. Resolving any conflicts manually might be necessary.

Resolving Conflicts

If there’s a conflict, Git will pause the rebase so that you can resolve it. Once resolved, you can continue the rebase:

git add .
 git rebase --continue

If at any point you’d like to halt the rebase, you can do so with the command:

git rebase --abort

Interactive Rebase

An interactive rebase allows you to modify commits in the process. To start an interactive rebase:

git rebase -i HEAD~3

This command lets you manipulate the last three commits. You could squash commits, rewrite messages, or drop them altogether.

The Golden Rule of Rebasing

Never rebase commits that exist outside your repository. If you rebase commits that have been pushed and then push them again, the repository history will diverge, causing issues for others collaborating on the project.

Examples in Action

Let’s illustrate with examples:

Example 1: Basic Rebasing

#!/bin/bash
 # Assuming main is the base branch
 git checkout feature-branch
 git rebase main
 # Resolve any conflicts
 git add .
 git rebase --continue

Example 2: Interactive Rebase

#!/bin/bash
 git rebase -i HEAD~5
 # An editor opens showing the last 5 commits
 # You can now choose to squash, edit, or drop commits

Example 3: Skipping a Commit

#!/bin/bash
 git rebase -i HEAD~5
 # In the interactive list, change 'pick' to 'drop' for commits you want to remove

Conclusion

Rebasing can simplify your project’s history and streamline the merge process. That said, its power should be handled with care—especially when dealing with a shared repository. With practice, rebasing will become a natural extension to your Git toolset, enhancing your collaborative and individual coding efforts.

Always remember the golden rule of rebasing, and happy coding!