Last updated: Apr 5, 2024
Reading time·6 min
The Git error "Updates were rejected because the tip of your current branch is behind its remote counterpart" occurs when you try to push changes to a remote branch that is ahead of your local branch.
Here is the complete stack trace:
To github.com:bobbyhadz/bobbyhadz-git.git ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'github.com:bobbyhadz/bobbyhadz-git.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.
When working with Git, you use local and remote branches.
You can use the git branch
command to view your local branches.
git branch
You can use the git branch -r
command to view your remote branches.
git branch -r
Notice that the name of the remote branch is prefixed with origin/
. An easy
way to think about it is that origin
is equivalent to remote in this case.
If your local branch falls behind its remote counterpart, then the error is raised.
Your local branch might fall behind the remote branch for multiple reasons:
git rebase
command.Note: you should only use this approach if your colleagues haven't modified
the history of the remote branch with git rebase
.
The git rebase command is used to move or combine a sequence of commits to a new base commit.
The command changes the commit history, so you shouldn't use git pull
in case
git rebase
is used.
This can cause duplicate or unordered commits and a merge commit.
If your colleagues haven't used the git rebase
command, integrate the remote
changes before pushing.
Run the following commands from your terminal.
git config pull.rebase false git pull origin <your_remote_branch> git status # Resolve Merge Conflicts (if any) git add . git commit -m 'YOUR MESSAGE' git push origin <your_remote_branch>
Make sure to replace the placeholder with the name of your remote branch.
The git config pull.rebase
command is used to set the default Git strategy
when pulling from the remote.
The git pull
command fetches and downloads the content of the remote branch
and updates the local repository automatically.
If you get merge conflicts when issuing the git pull
command, you have to
follow the instructions to pick which changes you want to keep.
An alternative approach to solving the error is to use the --force option.
By default, the git push
command refuses to update a remote that is behind the
local branch.
The --force
(or -f
) flag disables these checks and can cause the repository
to lose commits.
The git push -f
command basically overwrites the remote branch with the
contents of your local branch.
If you simply want to override the remote with your local changes, use the
git push
command with the -f
flag.
git push -f origin master
Make sure to replace master
with the name of your remote branch.
For example, git push -f origin main
if your remote branch is called main
.
Conversely, if you need to overwrite all changes in your local branch with the
changes from the remote branch, use the git reset --hard
command.
Note that this would wipe all your local changes.
git reset --hard origin/master
Make sure to replace master
with the name of your remote branch.
The git reset --hard command resets the index and the working tree.
Changes to the tracked files are discarded and untracked files or directories are deleted.
An alternative approach to solve the error is to:
Note that a soft reset doesn't delete your files. It simply undoes the specified commits.
Stash (shelve) your changes.
Do a git reset --hard
to overwrite the local branch with the changes from
the remote.
Use the git stash pop
command to un-stash your changes and commit them.
Let's start with the git reset --soft
command.
Use the command to undo the last N commits so we can stash the changes.
You can use the git log
command if you need to see your git history.
git log
Check how many times you've committed locally and use the
git reset --soft HEAD~N
command.
git reset --soft HEAD~2
If you run the git status
command, you will see the files that you modified in
the specified number of commits in the staging area.
git status
git stash
command to temporarily shelve the changes you've made in
the last N commits.git stash
If you run the git status
command after running git stash
your working tree
will be clean.
git reset --hard origin/master
Make sure to replace master
with the name of your remote branch, e.g. main
.
At this point, your local repository is up to date with the remote (other than the saved stash).
git stash pop
command.git stash pop
The git stash pop
command re-applies the most recently created stash.
git add . git commit -m 'your message' git push origin <your_remote_branch>
If you need to add your changes in interactive mode, use the git add --patch
command.
git add --patch
The interactive session will prompt you if you want to stage each change.
An alternative approach is to push your changes to a different remote branch.
git checkout -b
command to create a new local branch.git checkout -b your-new-branch
git push origin your-new-branch
Now you can either merge the changes or create a pull request.
There are multiple ways to solve the error "Updates were rejected because the tip of your current branch is behind its remote counterpart":
git stash
to stash your local changes and re-apply them after doing
a hard reset.