Read Anthropic’s case study about Graphite Reviewer

Force pushing after a Git rebase

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

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


This guide will explain how to safely perform a rebase and force push in Git, covering various scenarios and best practices.

git rebase is a command in Git that re-applies commits on top of another base tip. It's commonly used to create a cleaner, more linear project history by rearranging commits and eliminating the need for unnecessary merge commits.

  1. Ensure your branch is up to date with the remote: Before starting the rebase, make sure your branch has the latest changes from the remote branch. This helps avoid conflicts during the rebase.

    Terminal
    git fetch origin
    git rebase origin/main

    Replace main with the branch you are rebasing onto.

  2. Perform the rebase: Start the rebase process by specifying the base branch:

    Terminal
    git rebase -i origin/main

    This command will open an interactive rebase session where you can choose which commits to rebase.

  3. Resolve any conflicts: If there are conflicts, Git will pause the rebase and allow you to fix them. After resolving any conflicts, use:

    Terminal
    git add .
    git rebase --continue

    Repeat this until all conflicts are resolved and the rebase completes. For a more detailed walkthrough see this guide on resolving conflicts in Git.

  4. Force push to remote: After successfully rebasing locally, you'll need to force push to update the remote repository:

    Terminal
    git push origin <branch-name> --force

    Replace <branch-name> with your branch. This command is necessary because the history of your local branch has diverged from the remote branch. In order to complete the rebase, you must rewrite the history of the remote repository using git push --force. This will overwrite the history of the remote branch with the edited rebased history of the local branch.

  • Git force push after rebase: This is required when you have already pushed the branch to a remote repository and then performed a rebase. Since the commit history changes, a simple push will be rejected, and you need to force push.

  • Git rebase without force push: If you're rebasing a branch that hasn't been pushed to a remote, or you're working on a personal branch that no one else is using, you might not need to force push. However, this is rare in collaborative environments.

  • Git rebase and push to remote: After rebasing, pushing to a remote typically requires force pushing due to changes in commit history.

For further reading, see this guide on rebasing in Git, or see the official Git documentation.

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

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2