Git push rejected after feature branch rebase
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When working with Git, the git push rejected error is a common issue developers face, especially after rebasing a feature branch. Rebasing is a powerful tool for streamlining a series of commits into a more linear history. However, it can introduce complications if the rebased branch diverges from its remote counterpart. This article will explore why a git push might be rejected after a rebase, how to resolve this issue, and how to prevent it in the future.
Understanding git push Rejection
The Problem
A git push is typically rejected if the remote branch has diverged from your local branch. Rebase rewrites the commit history by creating new commits for each commit in your branch, hence the divergence. If the remote has changes (even just the presence of the old commits now rewritten), Git will refuse to push as a safeguard against overwriting the existing commit history without explicit permission.
Why It Happens
- Rewriting History: After a rebase, your branch has a different commit history compared to the remote branch.
- Diverging Histories: The remote still contains the pre-rebased commits alongside any additional changes if collaboration occurred simultaneously.
- Protection Against Data Loss: Git prevents pushing these changes to avoid losing any commits unintentionally.
Resolving git push Rejected
When you encounter this issue, your task is to reconcile the histories before pushing again. Here’s how you can fix this problem.
Step 1: Understand the Changes
First, understand the extent of changes due to the rebase. Use the git log command to examine your commit history and compare it with the remote using git log origin/branch-name.
Step 2: Force Push with Care
The most straightforward, albeit potentially dangerous, solution is to force push the rebased commits:
Why Use --force?
The --force option allows you to push changes even when they aren't fast-forward, replacing the commit history on the remote with your rebased branch.
Risks:
- Overwrites Remote History: Can remove others' changes made on the remote.
- Coordination Required: If teammates have based work on the old commits, they may need to manually reconcile their changes.
Step 3: Revise Collaboration Strategy
To mitigate the risk associated with force pushes:
- Coordination: Inform and coordinate with team members before force-pushing.
- Feature Branches: Limit force-pushes to feature branches where collaboration is minimal.
- Push Policy: Establish clear guidelines for when forced pushes are acceptable in your team.
Alternative: Use --force-with-lease
To reduce risks associated with a force push, use:
How It Helps:
This variant checks if the branch on the remote has changed since your last fetch before forcing the push, offering a slightly safer alternative.
Preventing Future Rejected Pushes
Best Practices for Rebasing and Pushing
- Frequently Sync With Remote: Regularly pull and merge changes from the remote to minimize divergence.
- Use Feature Branches: Isolate rebasing and other history-altering activities to feature branches.
- Rebase Feature Branch Locally: If possible, limit rebasing to local branches that have not been pushed, reducing the risk of rejected pushes.
Compare: Rebase vs. Merge
| Method | Rewriting History | Ease of Collaboration | Clean History |
| Rebase | Yes | Moderate | Linear |
| Merge | No | Easy | Non-linear |
Conclusion
A git push rejected error after rebasing can be frustrating, but understanding its causes, alongside carefully managing Git history, can mitigate its impact. Leveraging strategies such as collaboration policies, feature branching, and careful use of force-pushing can go a long way in ensuring smooth team workflows and effective use of git rebase.

