68

I give up! Whenever I try to push I get a stupid:

! [rejected]        master -> master (non-fast forward)
error: failed to push some refs to 'git@github.com:companyX/projectX.git'

Our team has a new git setup. Instead of making private branches I now Forked our main repository (on github) to create my own copy.

At some point what I did was:

$ git fetch upstream master:upstreammaster

So here is my current setup::

$ git branch
master
* upstreammaster

$ git remote -v
origin  git@github.com:userX/projectX.git
upstream    git@github.com:companyX/projectX.git

where userX is my private repository.

So I go and make some changes to my upstreammaster branch, and the PULL from "upstream master". Everything merges and stuff:

$ git pull upstream master
remote: Counting objects: 95, done.
remote: Compressing objects: 100% (60/60), done.
remote: Total 60 (delta 54), reused 0 (delta 0)
Unpacking objects: 100% (60/60), done.
From git@github.com:companyX/projectX
 * branch            master     -> FETCH_HEAD
Merge made by recursive.
stuff                      |  165 ++++++++++++--------
stuff                      |   35 ++--
stuff                       |  107 ++++++++++---
stuff                       |  105 ++++++++++---
stuff             |   24 ++--
stuff               |    9 +-
stuff                   |   53 +++----
stuff            |   44 +++---
stuff              |   52 +++----
stuff |   32 +----
stuff          |    4 +-
 stuff             |  138 ++++++++---------
stuff     |   58 ++++----
stuff    |  115 ++++++++------
stuff          |    5 +-
stuff                       |   39 ++---
stuff                        |   28 ++--
 17 files changed, 560 insertions(+), 453 deletions(-)

but then when I try to do:

$ git push upstream master
To git@github.com:companyX/projectX.git
 ! [rejected]        master -> master (non-fast forward)
error: failed to push some refs to 'git@github.com:companyX/projectX.git'

Any help would be greately appreciated! If you need clarification please ask, I will reply!

Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
Andriy Drozdyuk
  • 58,435
  • 50
  • 171
  • 272
  • @drozzy: Do you know why Git does not accept your command? Your command once worked for me. After I moved Git to another folder in my Mac, I get a similar error message. – Léo Léopold Hertz 준영 Apr 07 '09 at 19:49
  • no sorry i don't know. Right now I mostly do "git push upstream master", after having merged my other branch into upstreammaster. – Andriy Drozdyuk Apr 07 '09 at 21:13
  • See also [Git push non-fast-forward updates were rejected](http://stackoverflow.com/questions/4684352/whats-a-fast-forward-in-git). –  May 18 '14 at 17:06
  • Check your folder permissions on the git-servers repo folder for write-access (also for subdirectories!) – Jonathan May 17 '17 at 10:26
  • git push can be rejected if you have two branches with similar name, in my case my second branch was named: branch1.name()+ '/specification'. Git rejected it and i solved my problem thanks to @Pat Notz –  Oct 19 '18 at 15:23

9 Answers9

28

When doing a push, try specifying the refspec for the upstream master:

git push upstream upstreammaster:master
Jarret Hardie
  • 95,172
  • 10
  • 132
  • 126
20

Jarret Hardie is correct. Or, first merge your changes back into master and then try the push. By default, git push pushes all branches that have names that match on the remote -- and no others. So those are your two choices -- either specify it explicitly like Jarret said or merge back to a common branch and then push.

There's been talk about this on the Git mail list and it's clear that this behavior is not about to change anytime soon -- many developers rely on this behavior in their workflows.

Edit/Clarification

Assuming your upstreammaster branch is ready to push then you could do this:

  1. Pull in any changes from the upstream.

    $ git pull upstream master

  2. Switch to my local master branch

    $ git checkout master

  3. Merge changes in from upstreammaster

    $ git merge upstreammaster

  4. Push my changes up

    $ git push upstream

Another thing that you may want to do before pushing is to rebase your changes against upstream/master so that your commits are all together. You can either do that as a separate step between #1 and #2 above (git rebase upstream/master) or you can do it as part of your pull (git pull --rebase upstream master)

Pat Notz
  • 208,672
  • 30
  • 90
  • 92
  • How do you mean "merge back to a common branch and then push"? Which is the common branch in this case? – Andriy Drozdyuk Mar 09 '09 at 12:02
  • I had the same problem after checking out and working on a branch, and pushing changes that included an update to a file that had been deleted in the master branch (which I had not touched at all). Another developer tried some Git commands I didn't catch, but the problem remained: `git status` showed that my branch was clean, `git pull` did nothing, but the command `git push` still output `error: failed to push some refs...`. I finally got the problem fixed like this: 1. `git checkout master`, 2. `git pull`, and 3. `git checkout the_branch_name`. – Teemu Leisti Sep 12 '12 at 08:01
  • er... well, and what happens when you do the first command (git pull upstream master) and you have a conflict in the pull??? You have to solve the conflict before going on?? – Raul Luna Jun 11 '13 at 13:55
14

First use

git pull https://github.com/username/repository master

and then try

git push -u origin master
Pang
  • 9,564
  • 146
  • 81
  • 122
user2368055
  • 410
  • 6
  • 14
13

First, attempt to pull from the same refspec that you are trying to push to.

If this does not work, you can force a git push by using git push -f <repo> <refspec>, but use caution: this method can cause references to be deleted on the remote repository.

netflux
  • 3,648
  • 6
  • 37
  • 40
10

Actually I got the same error but the below comment worked for me

git push -f origin master
Aman Gupta
  • 658
  • 1
  • 8
  • 14
  • 3
    Be aware that this solution will remove commits (possibly including work accomplished) in the target branch. – edthedev Oct 15 '21 at 14:14
3

Is your repository at "upstream" a bare repository? I got the same error, but when I change to bare they no longer happen.

alekwisnia
  • 2,314
  • 3
  • 24
  • 39
2

If nothing works, try:

git pull --allow-unrelated-histories <repo> <branch>

then do:

git push --set-upstream origin master
Rahul Bali
  • 692
  • 10
  • 28
1

If push request is shows Rejected, then try first pull from your github account and then try push.

Ex:

In my case it was giving an error-

 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/ashif8984/git-github.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

****So what I did was-****

$ git pull
$ git push

And the code was pushed successfully into my Github Account.

knittl
  • 246,190
  • 53
  • 318
  • 364
Ashif
  • 11
  • 2
0

My colleague had the additional error: Ask a project Owner or Maintainer to create a default branch

It turned out he didn't have enough permissions in the git repo (Gitlab). He was able to push when his access level was upgraded.

Adrian C
  • 71
  • 3