Skip to content
DeveloperMemos

A Quick Look at `git rebase`

git, version control2 min read

One of the essential commands in Git is git rebase. It allows you to modify and integrate changes from one branch onto another, providing a flexible approach to managing your project's commit history. In this article, we will explore git rebase and its practical usage with some examples.

What is Git Rebase?

Git rebase is a command used to apply changes from one branch onto another by altering the commit history. Unlike merging, which creates a new commit to combine changes, rebasing modifies the existing commits. It achieves this by moving, combining, or removing commits to create a linear sequence of changes. This can result in a cleaner and more organized commit history.

The Basics of git rebase

Before diving into the examples, let's cover the basic syntax of git rebase:

1git rebase <branch>

This command applies the commits from <branch> onto the current branch. The commits are replayed on top of the target branch, resulting in a linear history.

Example 1: Rebasing Feature Branch onto Master

Let's consider a common scenario where you have been working on a feature branch and want to integrate your changes onto the main branch (master). To achieve this, you can use git rebase as follows:

1# Switch to the feature branch
2git checkout feature
3
4# Fetch the latest changes from master
5git fetch
6
7# Replay the feature branch commits on top of master
8git rebase master

In this example, the git rebase master command applies the commits from the feature branch onto the master branch. The resulting commit history will be a linear sequence of changes, with your feature branch changes on top of the master branch.

Example 2: Interactive Rebase to Squash Commits

Another powerful feature of git rebase is interactive rebasing, which allows you to modify individual commits during the rebase process. This can be useful for cleaning up your commit history, combining small and related commits into more meaningful ones. Let's see an example:

1# Start an interactive rebase
2git rebase -i HEAD~3

In this example, we start an interactive rebase of the last three commits using the -i flag. This opens an editor where you can specify the actions to perform on each commit. For instance, you can choose to squash multiple commits into one or edit commit messages.

Example 3: Resolving Conflicts During Rebase

Sometimes conflicts may arise when rebasing branches, especially if both branches have made conflicting changes to the same file. Git makes it straightforward to resolve these conflicts during the rebase process. Here's how you can handle conflicts:

1# Start the rebase
2git rebase target_branch
3
4# Resolve conflicts by editing the conflicting files
5# Use `git add` to mark the resolved files as ready
6
7# Continue the rebase
8git rebase --continue

During a rebase, if conflicts occur, Git pauses the rebase process and informs you about the conflicting files. You can then manually edit those files to resolve the conflicts and use git add to stage the resolved files. Once all conflicts are resolved, you can continue the rebase using git rebase --continue.

Summing Up

Git rebase is a versatile command that allows you to rewrite commit history and integrate changes from one branch onto another. Its ability to create a linear commit history and perform interactive rebasing makes it a powerful tool in managing your codebase. Happy Coding!