Read Anthropic’s case study about Graphite Reviewer

Rebasing stacked branches in Git

Sara Verdi
Sara Verdi
Graphite software engineer
Try Graphite


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


In Git, stacked branches refer to a scenario where multiple branches are dependent on one another in a sequence. Each branch can represent incremental changes that build on top of the previous one, allowing you to continue building without being blocked on code review. Rebasing stacked branches allows you to integrate changes from one branch into another, ensuring a clean and linear project history.

The git rebase command is used to move or combine a sequence of commits to a new base commit. This operation can help maintain a cleaner project history by applying changes in a linear fashion. When dealing with stacked branches, rebasing becomes crucial as it allows you to reapply commits from dependent branches onto a new base to ensure that they incorporate the latest changes from the main branch or other branches in the stack.

  • Linear history: Rebasing results in a cleaner project history compared to merging, making it easier to follow changes.
  • Easier conflict resolution: Rebasing allows you to resolve conflicts as you reapply each commit, reducing complexity in multi-branch scenarios.
  • Focused changes: Each commit in a rebase focuses on a single change, making it easier to understand and review.

Before rebasing stacked branches, it's essential to understand the order of your branches and the dependencies between them. For instance, consider the following branches in your project:

  • main: the main branch containing stable code.
  • feature-A: a branch for the first feature.
  • feature-B: a branch that builds on feature-A.
  • feature-C: a branch that builds on feature-B.

You can visualize the branches as follows:

Terminal
main
├── feature-A
│ └── feature-B
│ └── feature-C

Make sure you have the latest changes from the remote repository. This is particularly important if your main branch has received updates since you branched off.

Terminal
git fetch origin

You will start by checking out the topmost branch in your stack, which in this case is feature-C.

Terminal
git checkout feature-C

Next, initiate the rebase process by specifying the branch you want to rebase onto. In this example, we will rebase feature-C onto the latest changes in feature-B.

Terminal
git rebase feature-b

During the rebase, you may encounter conflicts. Git will pause and allow you to resolve these conflicts. You can check the status of the rebase with:

Terminal
git status

Once you've resolved the conflicts in your files, stage the changes:

Terminal
git add <file>

Then continue the rebase process:

Terminal
git rebase --continue

Repeat these steps for any further conflicts that arise.

After rebasing feature-C, repeat the process for feature-B and then feature-A in sequence. Here’s how you can do this for feature-B:

Terminal
git checkout feature-B
git rebase feature-A

Follow the same conflict resolution process. Once done, proceed to feature-A:

Terminal
git checkout feature-A
git rebase main

Rebasing a chain of branches can be particularly useful when you have multiple features that depend on one another. By rebasing in this manner, you ensure that all branches incorporate the latest updates from the main branch, and their changes remain relevant and up to date.

Graphite provides powerful tools to accelerate and simplify your Git workflows, particularly when it comes to managing stacked branches. One notable feature is Graphite's ability to stack merge, which simplifies the process of rebasing and merging multiple dependent branches.

With Graphite's stack merge functionality, you can automatically rebase and merge stacked branches right from the CLI. This feature also ensures that each rebase and merge operation is tracked effectively, allowing you to see the relationships between your branches at a glance.

Additionally, Graphite’s automation capabilities help streamline your rebasing process. You can automate the rebase and merge workflows, making it easier to handle multiple branches simultaneously without losing track of changes. By using Graphite, you can focus more on development while the tool manages the complexities of branch dependencies, reducing the potential for errors and improving overall efficiency in your Git operations.

  • Rebase regularly: Keep your branches up to date with the main branch to minimize conflicts and keep your work relevant.
  • Document changes: Keep track of significant changes or decisions made during the rebase process, especially in a team setting.
  • Use feature flags: Consider using feature flags to control the rollout of new features in stacked branches, allowing for safer deployments.

By following these steps and best practices, you can effectively manage your Git repository and leverage the power of rebasing stacked branches to maintain a clean and organized project history.

Built for the world's fastest engineering teams, now available for everyone