Chuck's Academy

Git Branching and Merging

Rebase in Git: Concepts and Uses

Rebase in Git: Concepts and Uses

Rebase in Git is a powerful operation that allows you to rewrite the commit history of a branch. Unlike merge, which combines changes while keeping the original commits, rebase applies the commits of one branch onto another, creating a more linear and clean history.

What is Rebase?

The rebase aims to move or reassign the base of a branch. Essentially, it "replays" a series of commits from one point to another.

Example:

Suppose we have the following situation:

If you rebase feature onto main:

bash

The result will be:

Basic Rebase Command

To perform a rebase:

bash

Example:

bash

This reprocesses the commits from feature-xyz onto main.

Types of Rebase

Interactive Rebase

Interactive rebase (rebase -i) allows greater customization and control over the rewriting of commits. You can edit, combine, reorder, and delete commits.

To start an interactive rebase:

bash

Example:

bash

This will start an interactive rebase that includes the last three commits.

During interactive rebase, Git opens a text editor with a list of commits and commands you can use to modify the history.

Common Commands in Interactive Rebase:

  • pick - Use the commit.
  • reword - Edit the commit message.
  • edit - Pause to edit the commit.
  • squash - Combine the commit with the previous one.
  • fixup - Similar to squash but discard the commit message.
  • drop - Remove the commit.

Conflict Resolution during Rebase

Like a merge, conflicts can arise during a rebase. Git pauses the process and allows you to resolve conflicts manually.

Steps to Resolve Conflicts during Rebase:

  1. Identify the conflicts: Git will mark the conflicting files.
  2. Resolve the conflicts: Manually edit the conflicted files.
  3. Mark the resolved files:
    bash
  4. Continue with the rebase:
    bash

If you decide not to proceed with the rebase:

bash

Comparison between Merge and Rebase

Merge:

  • Preserves the original commit history.
  • Creates merge commits.
  • Useful for integrating large changes and team collaboration.

Rebase:

  • Linear and cleaner history.
  • Doesn't create merge commits.
  • Useful for maintaining a simple and orderly history.

When to Use Rebase

  1. Clean History: Maintains a more readable and clean history.
  2. Rebasing Local Features: Before publishing a feature branch to keep a more coherent history.
  3. Updating a Branch: Bringing changes from the main branch without introducing merge commits.

Good Practices with Rebase

  • Avoid rebase in public branches: Can cause collaboration issues.
  • Use rebase for your local work: Keep your history clean before sharing.

Summary

  1. Concept of Rebase: Moves commits from one branch onto another.
  2. Basic Command: git rebase <base-branch>
  3. Interactive Rebase: git rebase -i <base-commit>
  4. Conflict Resolution: Similar to merge, but use git rebase --continue
  5. Comparison with Merge: Linear history vs. preservation of original commits.
  6. Good Practices: Mainly use for local work and avoid in public branches.

Rebase offers an effective way to maintain a clean and organized history, essential in collaborative development environments. In the next chapter, we will explore when to use merge versus rebase.


Ask me anything