• Register
0 votes
1.7k views

Problem :

I am a beginner to Git . If I do the git push for the first time then it works fine for me and creates the remote branch also. But if I try to push for the second time for some eleventh hour updates then I get the below error:

Error: failed to push some refs to 'https://github.limeade.info/Limeade/product.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.

I am struggling with this error from past couple of days. Can somebody guide me in fixing it?

2.3k points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

This issue happened to me very recently. I will guide you through the solution.

I just made the pull request to our master yesterday the way I usually do it.

One of my colleagues was just reviewing my work on it today and he saw that it was out of the sync with the master branch so with clear purpose of just helping me he simply merged my master against the branch on which I was working.

And he forgot to tell me that.

Then when I tried to merge master locally and I tried to push it, but it failed and gave the error which you are facing.. The reason was my colleagues merged with the master created one extra commit which I did not had locally.

So the very simple solution on it is Pull down the own branch so that I can have the extra commit. Then just push it back to the remote branch.

So I literally did below steps on my branch :

git pull

git push

5k points
0 votes

Solution:

The -f is actually required because of the rebase. Whenever you do a rebase you would need to do a force push because the remote branch cannot be fast-forwarded to your commit. You'd always want to make sure that you do a pull before pushing, but if you don't like to force push to master or dev for that matter, you can create a new branch to push to and then merge or make a PR.

Ensure that your local branch FixForBug is not ahead of the remote branch FixForBug pull and merge the changes before pushing.

git pull origin FixForBug
git push origin FixForBug

 

In case you want to avoid having to use -f, then you can use just

git pull

Instead of

git pull --rebase

 

The non-rebase will fetch the changes from origin/dev and merge them into your FixForBug branch. Then, you will be able to run

git push origin FixForBug

without using -f.

Let's assume the upstream branch is the one that you forked from and origin is your repo and you want to send an MR/PR to the upstream branch.

You already have let's say about 4 commits and you are getting Updates were rejected because the tip of your current branch is behind.

Here is what I did

First, squash all your 4 commits

git rebase -i HEAD~4

You'll get a list of commits with pick written on them. (opened in an editor)

pick fda59df commit 1
pick x536897 commit 2
pick c01a668 commit 3
pick c011a77 commit 4

To

pick fda59df commit 1
squash x536897 commit 2
squash c01a668 commit 3
squash c011a77 commit 4

After that, you can save your combined commit

Following this

You'll need to stash your commit

git reset --soft HEAD~1
git stash

 

now rebase with your upstream branch

git fetch upstream beta && git rebase upstream/beta

 

Now pop your stashed commit

git stash pop

 

Another way of solution

Let's assume the upstream branch is the one that you forked from and origin is your repo and you want to send an MR/PR to the upstream branch.

You already have let's say about 4 commits and you are getting Updates were rejected because the tip of your current branch is behind.

Here is what I did

First, squash all your 4 commits

git rebase -i HEAD~4

You'll get a list of commits with pick written on them. (opened in an editor)

example

pick fda59df commit 1
pick x536897 commit 2
pick c01a668 commit 3
pick c011a77 commit 4

to

pick fda59df commit 1
squash x536897 commit 2
squash c01a668 commit 3
squash c011a77 commit 4

After that, you can save your combined commit

Next Move

You'll need to stash your commit

Here's how

git reset --soft HEAD~1
git stash

now rebase with your upstream branch

git fetch upstream beta && git rebase upstream/beta

Now pop your stashed commit

git stash pop

commit these changes and push them

git add -A
git commit -m "[foo] - foobar commit"
git push origin fix/#123 -f

 

31.7k points