Handling Push Rejections
What you'll learn: How to fix the frustrating "rejected" error when Git won't let you push your changes to a remote repository.
The Problem: Non-Fast-Forward Errors
When you try to push your commits to a remote repository, Git sometimes refuses with an error like "Updates were rejected because the remote contains work that you do not have locally." This happens when someone else (or you from another machine) has pushed commits to the remote branch after you last fetched or pulled.
Think of it like trying to add a new chapter to a book, but someone already added a different chapter in that same spot. Git can't just overwrite their work—it needs you to reconcile both versions first.
Why This Happens
The remote branch has moved forward with new commits that aren't in your local history. Your local branch and the remote branch have diverged. Git prevents you from pushing because doing so would erase those commits someone else made, potentially losing their work.
The Solution: Fetch and Integrate
Before you can push, you need to bring those remote changes into your local repository:
- Fetch the changes:
git fetch origindownloads the new commits without modifying your working files - Integrate them: Use
git pull(which fetches and merges) or manually merge/rebase the remote branch into yours - Resolve any conflicts if your changes and theirs touched the same code
- Try pushing again: Now your history includes their commits plus yours, and Git will accept the push
When It's Safe to Force Push
Never use git push --force unless you're absolutely certain you want to overwrite remote history—it's dangerous and can destroy others' work.
Key Takeaway: Push rejections protect collaborative work; always fetch and integrate remote changes before pushing to avoid losing teammates' commits.