64

I ran these commands below:

git add .
git commit -m 't'

Then, when running the command below:

git push origin development

I got the error below:

To git@github.com:myrepo.git
 ! [rejected]        development -> development (non-fast-forward)
error: failed to push some refs to 'git@github.com:myrepo.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.

Are there any ways to solve the error above?

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Tampa
  • 75,446
  • 119
  • 278
  • 425

15 Answers15

90

Your origin repository is ahead of your local repository. You'll need to pull down changes from the origin repository as follows before you can push. This can be executed between your commit and push.

git pull origin development

development refers to the branch you want to pull from. If you want to pull from master branch then type this one.

git pull origin master
akshay_rahar
  • 1,661
  • 2
  • 18
  • 20
Dan Lister
  • 2,543
  • 1
  • 21
  • 36
  • 8
    the `development` in the above message refers to the branch you want to pull from. So if you are using `master` branch, you would type in : `git pull origin master` – Myna Mar 20 '13 at 02:53
37

In my case Github was down.

Maybe also check https://www.githubstatus.com/

You can subscribe to notifications per email and text to know when you can push your changes again.

Oscar Wieman
  • 480
  • 4
  • 6
9

I have faced the same issue and resolved as follows (if you have a project in local folder then follow the steps):

  1. create a new repo in GitHub
  2. go to local folder and do "git init"
  3. git remote add origin (with your repo url) // simply copy from your repo
  4. git add -A
  5. git commit -m "your commit"
  6. git push -u origin master
Community
  • 1
  • 1
Umair Arshad
  • 97
  • 1
  • 4
7

I also got the error ! [remote rejected] main -> main (failure) error: failed to push some refs to '<repository>'.

Came to find out this is the reason:

enter image description here

Fiddle Freak
  • 1,923
  • 5
  • 43
  • 83
6

In my case. I had the error because I forgot to make a commit after create a repository on github into an existing project. So I solved:

git add .
git commit -m"commentary"

Then I was able to type:

git push -u origin master
iargdel
  • 61
  • 1
  • 5
5

I used this command and it worked fine with me:

>git push -f origin master

But notice, that may delete some files you already have on the remote repo. That came in handy with me as the scenario was different; I was pushing my local project to the remote repo which was empty but the READ.ME

Amado Saladino
  • 2,114
  • 23
  • 25
4

you can write in your console:

git pull origin

then press TAB and write your "master" repository

Epredator
  • 109
  • 3
  • 8
4

Try this:

  1. git push -u origin master
  2. git push -f origin master

Sometimes #1 works and sometimes #2 for me. I am not sure why it reacts in this way

1

In windows, you need to use double quotes "". So the command would be

git commit -m "t"

Tui Popenoe
  • 2,098
  • 2
  • 23
  • 44
1

In my case git push was trying to push more that just the current branch, therefore, I got this error since the other branches were not in sync.

To fix that you could use: git config --global push.default simple That will make git to only push the current branch.

This will only work on more recent versions of git. i.e.: won't work on 1.7.9.5

douglaslps
  • 8,068
  • 2
  • 35
  • 54
1

This command worked for me:

git push --set-upstream origin master

And if it doesn't work, please make sure that you are pushing on the current branch that you are on it.

App University>git branch
* master
  test

And after that, you must push your code on the master branch

 App University>git push origin master
Abbas Jafari
  • 1,492
  • 2
  • 16
  • 28
  • 1
    Not being on a valid branch gives this issue, as pointed out in this answer. You may experience this if you are using an IDE plugin for GIT. Sometimes these default to 'master' where as the more recent default is 'main'. So the IDE plugin may switch the repo to master or other branch - so when you do a manual commit you could see this issue as a result – Mark Parris Jan 29 '23 at 10:55
0
$ git fetch --unshallow origin
$ git push you remote name
CAFEBABE
  • 3,983
  • 1
  • 19
  • 38
sam.hu
  • 11
0

I have faced below error $ git push origin main error: src refspec main does not match any error: failed to push some refs to 'https://github.com/--------/git-init-sample.git'

Solution : I was not connected to git local repo https://github.com/login/oauth/authorize?response_type=

Once i connected error gone

$ git push origin main Enumerating objects: 3, done. Counting objects: 100% (3/3), done.

Rajvp
  • 1
0

This same error but with a different details can be related to changes to privacy settings in the repository. The details are very clear actually.

In example: I changed my profile settings to hide my email address and that has an effect in all my repositories. However you can keep that setting checked and uncheck "Block command line pushes that expose my email" option in the Email Setting section

Karmavil
  • 863
  • 10
  • 13
0

I could push with "--force":

git push origin --force
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129