Search code examples
gitgithubgit-branchgit-push

Git error about non fast forwarded changes to branch


Right now I have a repo called playGround
It has 3 commits recorded
My local copy of playGround is behind 1 commit
I also have a branch called SecondB which was copied from my local copy of playGround. Then on SecondB I made two changes so SecondB has a total of 4 commits to it.

git remote show origin:

HEAD branch: master
  Remote branches:
    SecondB tracked
    master  tracked
  Local branches configured for 'git pull':
    SecondB merges with remote SecondB
    master  merges with remote master
  Local refs configured for 'git push':
    SecondB pushes to SecondB (up to date)
    master  pushes to master  (local out of date)

My question is when I am on the SecondB branch and try to push it will give me this error:

! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'url for repo'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

unless I specify what to push to like : git push origin SecondB while I am on SecondB

How come I cannot simply do git push as I am on SecondB and have it push to the remote copy of SecondB?

OKAY NEW INFORMATION: Upon examining the errors further it seems that every time I try to git push it tries to push to the remote branch AND the remote MASTER. Why is this behavior happening? and how would I fix this?


Solution

  • When you run git push without other parameters, it tries to push all the local branches that are tracking a remote branch. In your example it's master and SecondB. But your local master is behind the remote master, so it cannot be pushed. That's why the command fails.

    You can only push with git push if all tracking branches are ahead or in sync with their remote counterparts.