26

My current branch is my_branch. Trying to push the changes to the remote repo I get:

$ git push
Counting objects: 544, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (465/465), done.
Writing objects: 100% (533/533), 2.04 MiB | 1.16 MiB/s, done.
Total 533 (delta 407), reused 0 (delta 0)
To git@......git
   4ed90a6..52673f3  my_branch -> my_branch
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@......git'
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.

Trying to git pull results in:

$ git pull
Already up-to-date.

Why I get this error? How could I resolve this problem and perform git push successfully?

Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746

5 Answers5

35

My current branch is my_branch.

That is your problem. When you pull, Git is telling you that your branch my_branch is up to date, not master, which is behind origin/master making a fast-forward merge impossible.

In order to push master, you need to check out master and pull. This will merge in the changes waiting on origin/master and allow you to push your own changes.

git checkout master
git pull
# resolve conflicts, if any
git push
user229044
  • 232,980
  • 40
  • 330
  • 338
  • 3
    If you only want to push "my_branch": git push origin my_branch. – ysdx Jan 17 '12 at 22:24
  • 1
    Just to clarify the accepted answer some: `4ed90a6..52673f3 my_branch -> my_branch` indicates that `my_branch` has been updated successfully, but `! [rejected] master -> master (non-fast-forward)` indicates that the master branch has failed. This means that *some refs* have failed to be pushed to the repository, but some others *may* have gone through ok, like `my_branch`. – Ryan Bigg Jan 18 '12 at 01:12
  • This also can happen if you host the git repository by yourself and you are remotely checked (and logged in via terminal for example) into the remote branch you would like to push. In that case switch branch (on remote) and push again. – Markus Zeller Jun 06 '20 at 13:25
11

[If your master branch is already configured to rebase on pull, then you just need to do a pull on the master branch as is described in other answerd, but otherwise:]

If you get a non-fast-forward message, this means you can only push commits on top of the existing commits, but you're trying to do otherwise. Do a rebase on master before pushing (assuming the remote is called origin):

git checkout master
git pull --rebase origin master
git push

See also this question: git rebase and git push: non-fast forward, why use?

Community
  • 1
  • 1
The Nail
  • 8,355
  • 2
  • 35
  • 48
4

change developer permission to "Maintainer"

3

It could also be a Git Hook! That would give the same error.

Git Hooks run locally, so they can intercept and "pre-process" your commits before they are pushed.

More on Git Hooks here: https://githooks.com/ You should be able to find the hooks in the root of your repo, here: .git/hooks. Reading through the hook files may or may not not be helpful; they are full of advanced git commands and regexp. Use File Explorer/Finder to look, as files prefixed by a period (.git, .gitignore) are often hidden in IDE's.

I got this same error when trying to push a branch called feature/JIRA-123_description-text, and after some looking around I discovered that all the other project branches were named features/..., so I was just missing an s in the word features. Renaming the branch fixed it.

So, in case pulling master etc does not fix the issue, try looking at some other branch names and match the format. Or check docs to see if there are special branch naming policies, or if you've tried to push something in the wrong file format et cetera.

Olemak
  • 2,005
  • 1
  • 17
  • 20
2

what solved it for me:

go to https://github.com/settings/emails

unckeck: "Block command line pushes that expose my email"

the issue for me was: I had my email in the license

SwiftNinjaPro
  • 787
  • 8
  • 17