Programming

Git branch diverged after rebase

25 September 2026 · 7 min read

Git branch diverged after rebase

Navigating the complexities of Git can sometimes feel like solving a puzzle, especially when you encounter a Git branch diverged after rebase message. This common scenario arises when your local branch and its remote counterpart have different histories, often because a rebase operation has rewritten your local commit history. Understanding why this divergence occurs and, more importantly, how to resolve it gracefully is crucial for maintaining a clean, collaborative development workflow. This article will demystify the process, explain the underlying causes, and provide actionable strategies to bring your branches back into alignment, ensuring you can push your changes with confidence and avoid disrupting your team’s work.

Understanding Git Rebase and Branch Divergence

Git rebase is a powerful command that allows developers to integrate changes from one branch onto another by moving or combining a sequence of commits to a new base commit. While incredibly useful for maintaining a linear project history, it achieves this by rewriting commit history. Each commit in Git has a unique SHA-1 hash, which acts as its identifier. When you rebase, Git essentially creates new commits with new hashes, even if the changes introduced are identical. This is where the problem of a Git branch diverged after rebase originates.

The divergence typically happens when you rebase your local branch onto an updated upstream branch, and then attempt to push these rebased commits to a remote repository where the original, pre-rebase commits still exist. From the remote’s perspective, your local branch now contains a completely different set of commits, even if the content is functionally the same. The remote server detects this discrepancy and prevents a simple fast-forward merge, leading to the “diverged” error. This situation highlights a fundamental conflict between your rewritten local history and the established remote history, requiring careful intervention to resolve.

For example, imagine you have a local feature branch, feature/login, based on main. While you’re working, another developer pushes updates to main. You decide to rebase your feature/login branch onto the new main to get the latest changes cleanly. This creates new commits on your local feature/login. If you then try to git push origin feature/login without accounting for this history rewrite, Git will inform you that your branch has diverged, as the remote feature/login branch still points to your original commits before the rebase.

Identifying a Diverged Branch

Recognizing when your Git branch has diverged is the first step toward resolution. The most common indication is when you try to git push after a rebase and Git responds with an error message. Typically, this message will state something similar to: “Updates were rejected because the remote contains work that you do not have locally. This is usually caused by another repository pushing to the same ref. You may want to first integrate the remote changes (e.g., ‘git pull …’) before pushing again.” or more directly, “Your branch and ‘origin/your-branch’ have diverged, and have 1 and 2 different commits each, respectively.”

If you’re unsure, running git status can provide clues. It might tell you that your branch is “ahead” or “behind” its remote counterpart, or explicitly state that it has diverged. A visual inspection using git log --graph --oneline --all can also reveal differing commit histories between your local branch and the remote tracking branch (e.g., origin/your-branch). You’ll see two distinct lines of development where there should ideally be one linear path, signaling that the local commits have effectively branched off from the remote’s perspective.

Featured Snippet: When your Git branch diverged after rebase, it means your local branch’s commit history has been rewritten and no longer aligns with the remote branch’s history. This often occurs because rebase creates new commit IDs, making your local branch appear to have a separate lineage from the remote. The primary way to resolve this is by using git push --force-with-lease or git push --force, but only after carefully verifying that no critical work on the remote branch will be overwritten.

Another tell-tale sign is when a standard git pull command fails to perform a fast-forward merge or creates an unexpected merge commit, indicating that Git cannot simply integrate the remote changes without a more complex resolution. It’s crucial to pay close attention to Git’s output during these operations, as it often provides hints about the underlying issue. Understanding these messages will guide you toward the appropriate resolution strategy.

Infographic: Visualizing Git Branch Divergence and Resolution
Strategies for Resolving Diverged Branches ------------------------------------------

Once you’ve identified a diverged branch, several strategies can bring your branches back into sync. The choice depends heavily on your specific situation, especially whether your rebased commits have already been shared with others and if you’re certain about overwriting remote history. Always proceed with caution, particularly when using commands that rewrite remote history.

One of the safest and most common approaches, assuming your local rebase was intended and you are sure no one else has based work off the original remote commits, is to use a force push. However, a regular git push --force can be dangerous as it blindly overwrites the remote branch with your local history, potentially deleting work that others have pushed in the interim. A safer alternative is git push --force-with-lease. This command is a cautious form of force push; it will only update the remote branch if it’s in the state you expect it to be. If the remote branch has new commits that you haven’t incorporated into your rebase, --force-with-lease will fail, preventing accidental data loss.

Here’s how to safely push your rebased branch using --force-with-lease:

  1. Verify Your Local History: Use git log --oneline --graph to ensure your local branch history is exactly as you intend it to be after the rebase.
  2. Check Remote Status: Run git fetch origin to update your remote tracking branches without merging. This helps --force-with-lease detect if the remote has changed.
  3. Execute Force Push (with lease): Assuming your current branch is feature/xyz, run git push --force-with-lease origin feature/xyz. Git will then compare the remote’s current state with what it was when you last fetched. If they match, your local rebased history will overwrite the remote. If not, the push will be rejected, protecting against unexpected overwrites.

If you’ve rebased locally and then realize you shouldn’t have, or if others have already based work on the remote branch, then force-pushing is not an option. In such cases, you might consider either reverting your local rebase (using git reflog and git reset --hard to go back to the state before the rebase) or performing a git merge to integrate the remote’s history into your rebased branch, which will create a merge commit. This latter approach, while creating an extra merge commit, preserves all history and avoids overwriting shared work. For more on Git’s powerful history manipulation tools, consult the official Git documentation on rebase.

Best Practices to Avoid Future Divergence

Preventing Git branch diverged after rebase issues is often simpler than resolving them. Adopting a few best practices can significantly reduce the likelihood of encountering these problems, especially in a team environment. The core principle is to be mindful of how and when you rewrite history, and how that impacts others.

Firstly, communicate effectively with your team. If you plan to rebase a shared branch, inform your collaborators. This allows them to fetch the changes and rebase their own work on top of your new history, or simply pull your changes carefully. As Atlassian explains in their Git Rebase Tutorial, “The golden rule Question & Answer :

I have rebased a branch locally which was already pushed.

Git is advising that my branch and remote have diverged and that:

“and have 109 and 73 different commits each, respectively”

Will pushing my branch resolve this - i.e. is this to be expected after a rebase?

When you rebase a branch, you have to rewrite the commits for any commit which is above the commits in the branch onto which you are rebasing. This is because one of the properties of a commit is its parent (or parents). When you rebase, you’re changing the parent of the oldest local commit on your branch - and thus changing the commit hashes of all of your local commits, since this change bubbles up through the commits transitively.

Since you’d already pushed the branch, you should have merged in the source branch, rather than rebasing against it. It is possible to “force push” your new branch (using the -f flag), but a normal push won’t work, because the integrity of the branches history will be disturbed. If you are collaborating with others on this branch, force pushing is a bad idea, as it will cause other collaborators to become very confused when their history suddenly doesn’t match.

TL;DR - If you’re not collaborating, push the branch using push -f. If you are, reset the branch to the previous state, and merge in the source branch, instead.