Fangyijie Wang

Git error - current branch is behind its remote counterpart

It’s been a long time since I started playing with Github, but today I was facing a really wired git error when I tried to push my changes to origin/master branch. Because I’m not a professional guy in terms of git, I spent some time to figure it out and fix it. Here is the git error I met.

The error is below
error: faild to push some refs to ‘[my-github-repository]’

git error I had when push codes

Based on above screenshot, the hint message says “Updates were rejected because the tip of your current branch is behind its remote counterpart”. It’s strange because in my repository there is only one branch “master”.

So I tried to checkout my current branch and also check git status, everything looks good.

Then I also tried to pull from remote origin/master branch.

But unfortunately I got the same error message.

same error message

Actually I googled the solution to this error. There is one risk solution I found but a easy way to solve current problem.

What you need to do is force your commit with below command:

$ git push -f

NOTE: This command has the risk of losing others changes. It can cause remote repository to lose commits.

In my case, I didn’t have other commits, so I use this way to solve the error.

force push

Meanwhile there is another safety way to solve the problem. If you use below commands to set upstream, when push codes to remote agian, the error will be gone.

git branch --set-upstream to=origin/master
git push origin <local_branch_name>

Also set upstream for pull

git branch --set-upstream to=origin/<local_branch_name> 
git pull origin <local_branch_name>

Hope them will help when you are facing the same git error with me.