Kex Kex - 2 months ago 9
Git Question

Cannot push new branch from checkout

I was doing some work on a branch that comes from

develop
. I decided I don't want to use the branch anymore so did
git checkout develop
and then deleted it with
git branch -D branch1.0
. Later I decided to create the branch again (there were no changes to develop) with
git checkout -b branch1.0
. However when I now try to push the new branch I get this following error:

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.


Why am I getting this? Any pointers would be really appreciated!

Answer

This is because your local branch is not in sync with the remote branch. You can either force push the local branch by using:

git push -f origin <branch>    

Use this with care. THIS WILL CAUSE THE REMOTE REPO TO LOSE THE CHANGES MADE BY OTHER COLLABORATORS.

If you want to preserve the commits made in the remote, (after saving your local commits) you sync your local branch with remote using the following:

git pull --rebase

This will place all your local commits on top of newly pulled changes. Following this a push to your remote repo won't raise any flags.

Recreating the branch with same name raises flags on push/pull, if you don't delete the remote tracking information. To fix it delete the remote tracking info by using git branch -d -r origin/<branch> as well.