How to reproduce it
- Add remote repository
- Stage and commit your local changes
- Push to Remote Repository
git push -u origin main
Output
To https://github.com/*.git
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/*.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
The error message means that your local branch is behind the remote branch and Git is preventing you from pushing your changes to avoid overwriting changes that already exist in the remote repository.
To resolve this, you need to pull the remote changes and integrate them into your local branch before you can push your local changes.
Solution
git fetch origin
git pull origin main
Output
…
Branch 'main' set up to track remote branch 'main' from 'origin'.
or
Output
* branch main -> FETCH_HEAD
fatal: Need to specify how to reconcile divergent branches.
You can use the --rebase
option to apply your local commits on top of the fetched commits or the --no-rebase
option to create a merge commit.
Using --rebase
git pull --rebase origin main
Output
* branch main -> FETCH_HEAD
Successfully rebased and updated refs/heads/main.
Try to use push again.
git push -u origin main
Output
…
Branch 'main' set up to track remote branch 'main' from 'origin'.