3

I have a local master branch, after trying some stuff, decided to go back to commit X, like 4 previous commits, then I make a new branch "master2" and this is now my main branch. When I try to do a new push (to my github) for my different branch it throws an error.

 git push -u origin master2
 ! [rejected]        master2 -> master2 (non-fast-forward)

How can i do a push to github for this new branch?

1
  • What's the current situation on Github? Does the branch exist? At what commit is it?
    – Joost
    Commented Feb 3, 2016 at 23:11

2 Answers 2

3

I'm not sure if I understand your problem completely, however, non-fast-forward pushes (which is your case) can be solved by adding -f switch (force) to the push command:

git push -fu origin master2

(however, using force push on e.g. Github is considered as a bad thing to do and there is a reason for that: you can mess up someone else repository, so you should do it only if you are sure that no one fetched the repository with the master2 branch pointing to whatever it was before forcepush)

1
  • 2
    However, you probably shouldn't do that.
    – SLaks
    Commented Feb 3, 2016 at 23:13
0

The "non-fast-forward" error tells you that the master2 branch already exists in your GitHub repo. I suggest that you check what commits are on this branch. You have two options:

  1. Merge the GitHub version of master2 with your local one:

    git pull origin master2
    

    If this runs without any conflicts, then you can simply push:

    git push origin master2
    

    If there were conflicts with the first command, then you need to resolve the conflicts and commit the changes:

    git commit
    

    Then you can push as before.

  2. You can also just overwrite the existing master2 branch:

    git push -f origin master2

    Warning: Be very careful about doing this, though, as it will cause headaches for anyone who has already pulled the master2 branch and merged or commited to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.