Graphite Reviewer is now Diamond

How to fix Git push rejected errors

Greg Foster
Greg Foster
Graphite software engineer
Try Graphite


Note

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


When working with Git and GitHub, you might encounter errors such as:

  • master rejected non-fast-forward
  • github push rejected
  • remote rejected
  • git failed to push some refs
  • git remote rejected
  • failed to push some refs to github

These errors typically occur when your local branch is out of sync with the remote branch. This guide provides step-by-step instructions to resolve these issues using Git and the Graphite CLI.

A common cause of push rejections is a non-fast-forward update. This happens when the remote branch has new commits that your local branch doesn't have. Git prevents you from overwriting these changes to avoid potential data loss.

Before pushing, ensure your local repository is up to date:

Terminal
git fetch origin

This command retrieves the latest commits from the remote repository without modifying your local branches.

After fetching, integrate the remote changes into your local branch.

Option A: Merge

Terminal
git merge origin/master

This merges the remote master branch into your local master branch.

Option B: Rebase

Terminal
git rebase origin/master

Rebasing rewrites your local commits on top of the remote commits, creating a linear history.

Choose the method that aligns with your project's workflow.

If there are conflicts during merge or rebase, Git will prompt you to resolve them. Open the conflicting files, make the necessary adjustments, then:

Terminal
git add <file>

After resolving all conflicts:

  • For merge:

    Terminal
    git commit
  • For rebase:

    Terminal
    git rebase --continue

Once your local branch is in sync with the remote:

Terminal
git push origin master

This should successfully update the remote branch.

Graphite has a CLI tool that streamlines Git workflows, especially for managing stacked changes.

If you haven't installed Graphite:

Terminal
brew install graphite-cli

Create a new branch:

Terminal
gt branch create <branch-name>

Commit your changes:

Terminal
gt commit -m "Your commit message"

Push your changes:

Terminal
gt submit

If you encounter a push rejection, Graphite will provide guidance on resolving the issue, often suggesting a rebase:

Terminal
gt rebase

After rebasing, push again:

Terminal
gt submit

Graphite simplifies the process of keeping your branches up to date and resolving conflicts.

  • Always pull or fetch the latest changes before starting new work.
  • Communicate with your team to coordinate changes and avoid conflicts.
  • Use feature branches to isolate your work from the main branch.

By following these steps and utilizing tools like the Graphite CLI, you can effectively resolve common Git push rejection errors and maintain a smooth workflow.

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