git rebase is a powerful command-line tool used in Git for rewriting commit history. It allows you to apply a series of commits on top of another base tip, effectively changing the order and structure of your project's history. This tutorial will guide you through the basics of using git rebase, including its syntax, common use cases, best practices, and real-world examples.
Before diving into how to use git rebase, it's important to understand what it does under the hood. When you run git rebase, Git will take all the changes that were committed on one branch and replay them on another base tip. This can be useful for several reasons:
The basic syntax for git rebase is as follows:
git rebase <branch>
This command will take the current branch and replay its commits on top of <branch>. For example, if you are on the feature-branch and want to rebase it onto main, you would run:
git rebase main
Interactive rebase provides more control over how commits are applied. It allows you to edit, squash, or reorder commits before they are applied to the base branch.
To start an interactive rebase, use the -i flag:
git rebase -i <branch>
This will open a text editor with a list of commits that will be rebased. Each commit is prefixed with pick. You can change pick to other commands like squash, reword, or drop.
Suppose you have the following commit history:
commit 3 (HEAD -> feature-branch)
Author: John Doe <john.doe@example.com>
Date: Mon Jan 1 12:00:00 2024 +0000
Add new feature
commit 2
Author: Jane Smith <jane.smith@example.com>
Date: Sun Dec 31 23:59:00 2023 +0000
Fix bug in existing feature
commit 1 (main)
Author: Alice Johnson <alice.johnson@example.com>
Date: Sat Dec 30 23:58:00 2023 +0000
Initial commit
If you want to squash the last two commits into one, you would run:
git rebase -i main
In the editor, change the list to:
pick 1 Initial commit
squash 2 Fix bug in existing feature
squash 3 Add new feature
Save and close the editor. Git will then combine these commits into one.
You can rebase onto a specific commit rather than a branch by using its hash:
git rebase <commit-hash>
This is useful when you want to apply your changes on top of a particular state of the project.
If you encounter conflicts and decide not to proceed with the rebase, you can abort it:
git rebase --abort
This will return your branch to its original state before the rebase started.
Backup Your Work: Always ensure you have backups of your work before starting a rebase. You can create a new branch as a backup:
git checkout -b backup-branch
Avoid Rebasing Public Branches: Do not rebase branches that are shared with others, as it can cause conflicts and confusion for collaborators.
Use Interactive Rebase Sparingly: While interactive rebase is powerful, use it judiciously to avoid rewriting history excessively.
Resolve Conflicts Promptly: If conflicts arise during a rebase, resolve them promptly to prevent the rebase from becoming too complex.
Test After Rebasing: Always test your code after a rebase to ensure that everything still works as expected.
Let's walk through a real-world example of using git rebase.
You are working on a feature branch and want to integrate the latest changes from the main branch while also squashing some commits for cleaner history.
Switch to Your Feature Branch:
git checkout feature-branch
Fetch Latest Changes from Main:
git fetch origin main
Start Interactive Rebase onto Main:
git rebase -i origin/main
Edit the Commit List: Suppose your commit list looks like this:
pick 1 Initial feature implementation
squash 2 Fix minor bug
squash 3 Add unit tests
Save and close the editor.
Resolve Conflicts (if any): If conflicts arise, resolve them and continue the rebase:
git add <resolved-files>
git rebase --continue
Test Your Changes: Ensure everything works as expected after the rebase.
Force Push to Remote (if necessary): Since you have rewritten history, you may need to force push your changes:
git push origin feature-branch --force
git rebase is a versatile tool that can help you manage and clean up your commit history. By understanding its syntax, common use cases, and best practices, you can effectively use git rebase to maintain a clean and linear project history. Always remember to back up your work and test thoroughly after rebasing to ensure everything functions correctly.