Rebasing is a powerful feature in Git that allows you to integrate changes from one branch into another. Unlike merging, which creates a new commit to combine the histories of two branches, rebasing re-applies commits from one branch onto another, effectively rewriting the commit history. This can result in a cleaner, more linear project history.

Key Concepts

  1. Rebase vs. Merge:

    • Merge: Combines the histories of two branches, creating a new commit.
    • Rebase: Moves or re-applies commits from one branch onto another, rewriting the commit history.
  2. Interactive Rebase: Allows you to edit, reorder, squash, or drop commits during the rebase process.

  3. Upstream Branch: The branch onto which you are rebasing your changes.

  4. Rebase Conflicts: Similar to merge conflicts, these occur when changes in the branches being rebased conflict with each other.

Practical Example

Scenario

You have a feature branch feature-branch and you want to rebase it onto the main branch to incorporate the latest changes from main.

Steps

  1. Checkout the Feature Branch:

    git checkout feature-branch
    
  2. Rebase onto Main:

    git rebase main
    
  3. Resolve Conflicts (if any): If there are conflicts, Git will pause the rebase process and allow you to resolve them. After resolving conflicts, you need to continue the rebase:

    git add <resolved-files>
    git rebase --continue
    
  4. Abort Rebase (if needed): If you decide to stop the rebase process, you can abort it:

    git rebase --abort
    

Example Code

Let's assume the following commit history:

  • main branch:

    A---B---C
    
  • feature-branch:

    A---B---C
         
    D---E

After rebasing feature-branch onto main, the history will look like this:

  • main branch:

    A---B---C
    
  • feature-branch:

    A---B---C---D'---E'
    

Interactive Rebase

Interactive rebase allows you to modify commits during the rebase process. For example, you can squash multiple commits into one.

  1. Start Interactive Rebase:

    git rebase -i HEAD~3
    
  2. Edit the Rebase Todo List: This will open an editor with a list of commits. You can change the commands to pick, squash, edit, etc.

    pick 1234567 Commit message 1
    squash 89abcde Commit message 2
    pick fedcba9 Commit message 3
    
  3. Save and Close the Editor: Git will then proceed with the rebase, applying your changes.

Practical Exercises

Exercise 1: Basic Rebase

  1. Create a new branch from main:

    git checkout -b new-feature
    
  2. Make some commits on new-feature.

  3. Rebase new-feature onto main:

    git rebase main
    
  4. Resolve any conflicts and complete the rebase.

Exercise 2: Interactive Rebase

  1. Create a new branch from main:

    git checkout -b interactive-feature
    
  2. Make three commits on interactive-feature.

  3. Start an interactive rebase to combine the last two commits:

    git rebase -i HEAD~3
    
  4. Edit the rebase todo list to squash the last two commits.

  5. Save and complete the rebase.

Solutions

Solution to Exercise 1

  1. Create a new branch:

    git checkout -b new-feature
    
  2. Make some commits:

    echo "Feature 1" > feature1.txt
    git add feature1.txt
    git commit -m "Add feature 1"
    
    echo "Feature 2" > feature2.txt
    git add feature2.txt
    git commit -m "Add feature 2"
    
  3. Rebase onto main:

    git rebase main
    
  4. Resolve conflicts if any, then:

    git add <resolved-files>
    git rebase --continue
    

Solution to Exercise 2

  1. Create a new branch:

    git checkout -b interactive-feature
    
  2. Make three commits:

    echo "Feature A" > featureA.txt
    git add featureA.txt
    git commit -m "Add feature A"
    
    echo "Feature B" > featureB.txt
    git add featureB.txt
    git commit -m "Add feature B"
    
    echo "Feature C" > featureC.txt
    git add featureC.txt
    git commit -m "Add feature C"
    
  3. Start interactive rebase:

    git rebase -i HEAD~3
    
  4. Edit the rebase todo list:

    pick 1234567 Add feature A
    squash 89abcde Add feature B
    pick fedcba9 Add feature C
    
  5. Save and complete the rebase.

Common Mistakes and Tips

  • Forgetting to Resolve Conflicts: Always resolve conflicts and continue the rebase process.
  • Rebasing Public Branches: Avoid rebasing branches that have been shared with others, as it rewrites history and can cause confusion.
  • Using --force with Caution: When pushing rebased branches, you may need to use --force or --force-with-lease. Use these options carefully to avoid overwriting others' work.

Conclusion

Rebasing is a powerful tool in Git that helps maintain a clean and linear project history. By understanding and practicing both basic and interactive rebasing, you can effectively manage and integrate changes in your projects. In the next topic, we will explore cherry-picking commits, another advanced Git operation.

© Copyright 2024. All rights reserved