Coding Ref

Updates were rejected because the tip of your current branch is behind

Updates were rejected because the tip of your current branch is behind

The git "Updates were rejected because the tip of your current branch is behind" error happens when the branch you are pushing to has changes made it to that are not in your branch. To solve the error, run the git pull command.

You might see an error that looks like this in the terminal when you try to push your git branch.

shell
Pushing to [email protected]:example-project/example-repo.git
To [email protected]:example-project/example-repo.git
  ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to '[email protected]:test-project/test-repo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Pull changes

Open your terminal and run the following command to pull changes from the remote branch.

shell
git pull
git push

# Push to a specified branch
git push origin RemoteBranch

If this doesn't work, pull changes from the remote branch and do a rebase. Run these commands in the terminal.

shell
git pull --rebase
git push

# Push to a specified branch
git push origin RemoteBranch

If the push does not work, try forcing the push, which may be necessary because of the rebase.

shell
git push -f

# Push to a specified branch
git push -f origin RemoteBranch

Overwrite your local changes

If this doesn't work, you can overwrite the changes in your local branch with the changes from the remote branch.

If you and the remote branch both made changes to the same lines of code, your changes will be destroyed.

Run the following command in your terminal.

shell
git reset --hard origin/example-branch

Conclusion

The git "Updates were rejected because the tip of your current branch is behind" error happens when the branch you are pushing to has changes made it to that are not in your branch. To solve the error, run the git pull command.