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:
- Identify the conflicts: Git will mark the conflicting files.
- Resolve the conflicts: Manually edit the conflicted files.
- Mark the resolved files:
bash
- 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
- Clean History: Maintains a more readable and clean history.
- Rebasing Local Features: Before publishing a feature branch to keep a more coherent history.
- 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
- Concept of Rebase: Moves commits from one branch onto another.
- Basic Command:
git rebase <base-branch>
- Interactive Rebase:
git rebase -i <base-commit>
- Conflict Resolution: Similar to merge, but use
git rebase --continue
- Comparison with Merge: Linear history vs. preservation of original commits.
- 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.
- Introduction to Git
- Initial Setup and Basic Workflow
- Basic Concepts of Branches in Git
- Creating and Deleting Branches
- Branch Navigation
- Branch Merging
- Resolución de Conflictos de Fusión
- Merge Strategies: Fast-Forward vs. Recursive
- Rebase in Git: Concepts and Uses
- Merge vs. Rebase: When to Use Each
- Remote Branches and Their Management
- Git Flow and Other Workflow Models
- Best Practices for Branching and Merging
- Advanced Tools and Commands
- Conclusion and Final Recommendations