Read Anthropic’s case study about Graphite Reviewer

Understanding and managing 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, a stacked branch refers to a set of branches that build upon each other in a linear sequence. Each branch contains commits that depend on the changes made in the previous branches. This strategy helps in organizing work, especially when dealing with multiple related features or fixes, allowing developers to keep building without being blocked on code review.

Stacked branches offer several advantages, including:

  • Modularity: Each branch can focus on a specific feature or fix, making it easier to manage changes.
  • Clarity: Developers can understand the dependencies between changes more clearly.
  • Easier collaboration: Multiple developers can work on different branches while maintaining a coherent codebase.

The Git branch stacking strategy involves creating a series of branches that depend on each other. Here’s a step-by-step example of how to implement this strategy:

  1. Create the main branch: This branch usually contains the stable code.

    Terminal
    git checkout main
  2. Create the first feature branch: Start by creating a new branch for the first feature.

    Terminal
    git checkout -b feature/first-feature
  3. Make changes and commit: Implement the feature and commit your changes.

    Terminal
    echo "First feature implementation" > feature.txt
    git add feature.txt
    git commit -m "Implement first feature"
  4. Create the second feature branch based on the first: Create a second branch that builds on the first feature.

    Terminal
    git checkout -b feature/second-feature
  5. Make changes in the second feature branch: Implement changes that depend on the first feature.

    Terminal
    echo "Second feature implementation" > second-feature.txt
    git add second-feature.txt
    git commit -m "Implement second feature that depends on the first"
  6. Continue stacking: Repeat the process for additional features as needed.

    Terminal
    git checkout -b feature/third-feature
    echo "Third feature implementation" > third-feature.txt
    git add third-feature.txt
    git commit -m "Implement third feature"

To manage stacked branches effectively, follow these practices:

Rebasing allows you to incorporate changes from one branch into another while maintaining a linear history. This is especially useful for stacked branches to ensure that they are up to date with the main branch.

Terminal
git checkout feature/third-feature
git rebase feature/second-feature

For more details, see this guide on rebasing stacks of branches.

Once a feature is complete, you can merge it back into the main branch. Make sure that you merge in the correct order to avoid dependency issues.

Terminal
git checkout main
git merge feature/first-feature
git merge feature/second-feature

Graphite is a powerful tool that accelerates and simplifies your workflow when managing stacked branches. It offers features like automatic rebasing, which allow you to manage multiple pull requests more effectively. With Graphite, you can:

  • Merge entire stacks of pull requests at once: Automatically queue and merge pull requests as dependencies are resolved.
  • Visualize branch dependencies: Graphite provides a clear view of how branches are stacked, helping to identify what needs to be merged next.
  • Streamline code review: Use Graphite’s review tools to manage feedback on each stacked branch efficiently.

Understanding and managing stacked branches in Git is essential for maintaining a clean and efficient codebase. By employing a git branch stacking strategy, you can ensure that your development process is organized and modular. Tools like Graphite enhance this workflow by automating merges and providing insights into branch dependencies. Embracing these practices can significantly improve collaboration and code quality within your development team.

Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

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