Symfony
Symfony

Reputation: 2439

! [rejected] master -> master (fetch first)

Is there a good way to explain how to resolve "! [rejected] master -> master (fetch first)'" in Git?

When I use this command $ git push origin master it display an error message.

! [rejected]        master -> master (fetch first)
error: failed to push some refs to '[email protected]:zapnaa/abcappp.git'

Upvotes: 172

Views: 643729

Answers (28)

Agung Sudrajat
Agung Sudrajat

Reputation: 328

I faced the same problem, this what I did to solve it:

  1. You made some changes and forgot to commit it. Then commit it first do the command in terminal: git add .
  2. then commit: git commit -am 'The Message'
  3. use this syntax: git pull --rebase origin master
  4. and then: git push origin master

Proof: enter image description here enter image description here

Upvotes: 1

Mahmoud Niazy
Mahmoud Niazy

Reputation: 49

This solution helped me :

git init git add README.md git add . git commit -m "first commit" git remote add origin https://github.com/userName/repoName.git git push --force origin master

Upvotes: -1

Ahmed Ramadan
Ahmed Ramadan

Reputation: 210

to Solve it added a "--force"

git push --force origin master

or pull it first and pull enter image description here

Upvotes: 2

Michael Horvilleur
Michael Horvilleur

Reputation: 149

If this is your first commit and you are trying to push local code to a new repository, you probably are getting this error because you added a README when you created the repo on github. Next time you create a new repo, do it WITHOUT adding a README on github.

Upvotes: 0

Wasif Mehmood
Wasif Mehmood

Reputation: 31

! [rejected] main -> main (fetch first) error: failed to push some refs to 'https://github.com/Wasif-M/Micard-Flutter.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.

Use this command after that:

git push -f origin main

i hope error will remove and your repository will commit to gitHub

Upvotes: 2

Diana
Diana

Reputation: 1419

It is very easy to fix, run the following command:

git fetch origin master

Upvotes: 0

Darwin
Darwin

Reputation: 2047

It is happen when you create a repository in GitHub and select README or .gitignore or license files. delete repasotory if it is empty and create it again without selecting README ,.gitignore or license files.

Upvotes: 1

Dilshad Shibin
Dilshad Shibin

Reputation: 78

! [rejected] master -> master (fetch first)

in this error showing time if you first checking your folder place correct path. And type: -

git push origin master --force

Upvotes: 3

majid asad
majid asad

Reputation: 409

This happens when your teammate have already committed. so his commit is on top. To avoid you do rebase (merge , fetch).

I solved my issue with the following.

  1. git pull --rebase origin master
  2. git push -u origin master

you can see logs git log.

Upvotes: 7

Raushan Kumar
Raushan Kumar

Reputation: 127

When we try to push the folder on Github using below-mentioned command

  $ git push origin master

and get the error like this:

To https://github.com/Raushan1156/QR-Code.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/Raushan1156/QR-Code.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.

Try this command to solve your issue, it's solved my error.

$ git push origin master --force

One Pic has been attached for a visual explanation. enter image description here

Upvotes: 2

Mahabub Azam
Mahabub Azam

Reputation: 51

I had faced this error while pressing the git push command.

  • The problem solved by simply adding -force after git push
  • Solution: git push -fource

enter image description here

Upvotes: 2

Raju Kumar
Raju Kumar

Reputation: 41

Quickest solution-

  1. DO a git log-> You will see someone probably has pushed some code post the time you had pulled the latest codebase.
  2. DO a git pull --rebase, this will first, rewind head to replay your work on top of it and then apply your committed changes to the same.
  3. You are now all set for a git push.

Upvotes: 4

ChrisLee
ChrisLee

Reputation: 1

This worked for me, since none of the other solutions worked for me. NOT EVEN FORCE!

https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/resolving-a-merge-conflict-using-the-command-line

Just had to go through Git Bash

cd REPOSITORY-NAME
git add .
git commit -m "Resolved merge conflict by incorporating both suggestions."

Then back to my cmd and I could: git push heroku master which in my case was the problem.

Upvotes: 0

Arijit Kundu
Arijit Kundu

Reputation: 547

The reason it happened in my case was when creating the GitHub rep link, I initialized it with README file

While creating Git remote do not initialize it with README file otherwise it would show err

Don't do that & it will definitely work fine Instead initialize it with the readme file if you wish to after pushing to the master branch

Upvotes: 0

Amit Kharel
Amit Kharel

Reputation: 154

You just have to mention your branch name along with your remote name.

git fetch origin
git merge origin/master

Upvotes: 0

user3826395
user3826395

Reputation: 13

I overcame this by checking-out a new branch like this :

# git checkout -b newbranch <SHA of master>

# git branch
* newbranch
  master

# git push -u <repo_url_alias> newbranch

You are left with 2 branch : Master and newbranch , that you can manage to merge later.

Upvotes: 1

Bhavdeep Kaur
Bhavdeep Kaur

Reputation: 99

Follow the steps given below as I also had the same problem:

$ git pull origin master --allow-unrelated-histories 

(To see if local branch can be easily merged with remote one)

$ git push -u origin master 

(Now push entire content of local git repository to your online repository)

Upvotes: 9

Amila Weerasinghe
Amila Weerasinghe

Reputation: 859

As it is stated in the Error message you have to "fetch first." This worked for me. Use the command:

  1. git fetch origin master

Then follow these steps to merge:

  1. git pull origin master
  2. git add .
  3. git commit -m 'your commit message'
  4. git push origin master

Upvotes: 30

Yamuna Bangaru
Yamuna Bangaru

Reputation: 49

This worked for me:

$ git add .
$ git commit -m "commit"
$ git push origin master --force

Upvotes: 3

divyang hirpara
divyang hirpara

Reputation: 31

Your error might be because of the merge branch.
Just follow this:

step 1 : git pull origin master (in case if you get any message then ignore it)
step 2 : git add .
step 3 : git commit -m 'your commit message'
step 4 : git push origin master

Upvotes: 2

Tanuj Chakraborty
Tanuj Chakraborty

Reputation: 59

You can use the following command: First clone a fresh copy of your repo, using the --mirror flag:

$ git clone --mirror git://example.com/some-big-repo.git

Then follow the codes accordingly:

Adding an existing project to GitHub using the command line

Even if that doesn't work, you can simply code:

$ git push origin master --force 

or

$ git push origin master -f

Upvotes: 4

Jarosław Cichoń
Jarosław Cichoń

Reputation: 542

Sometimes it happens when you duplicate files typically README sort of.

Upvotes: 2

linuxbandit
linuxbandit

Reputation: 2492

The answer is there, git is telling you to fetch first.

Probably somebody else has pushed to master already, and your commit is behind. Therefore you have to fetch, merge the changeset, and then you'll be able to push again.

If you don't (or even worse, if you force it by using the --force option), you can mess up the commit history.

EDIT: I get into more detail about the last point, since a guy here just gave the Very Bad Advice of using the --force option.

As git is a DVCS, ideally many other developers are working on the same project as you, using the same repository (or a fork of it). If you overwrite forcefully with your changeset, your repository will mismatch other people's, because "you rewrote history". You will make other people unhappy and the repository will suffer. Probably a kitten in the world will cry, too.

TL;DR

  1. If you want to solve, fetch first (and then merge).
  2. If you want to hack, use the --force option.

You asked for the former, though. Go for 1) always, even if you will always use git by yourself, because it is a good practice.

Upvotes: 215

Chrispher
Chrispher

Reputation: 383

It's likely that someone else (e.g. your colleague) has put commits onto origin/master that aren't in your local master branch, and you are trying to push some commits from your local branch to the server. In 99% of cases, assuming you don't want to erase their work from origin, you have two options:

2) Merge their changes into your local branch, and then push the merged result. git checkout master git pull # resolve conflicts here git push

(Note that git pull is essentially just a git fetch and a git merge in this case.)

1) Rebase your local branch, so that it looks like your colleague made their commits first, and then you made your commits. This keeps the commit history nice and linear - and avoids a "merge commit". However, if you have conflicts with your colleague's changes, you may have to resolve those conflicts for each of your commits (rather than just once) in the worst case. Essentially this is nicer for everyone else but more effort for you. git pull --rebase # resolve conflicts here git push

(Note that git pull --rebase is essentially a git fetch and a git rebase origin/master.)

Upvotes: 2

Aurelio A
Aurelio A

Reputation: 1847

try:

git fetch origin master
git merge origin master

After to wrote this code I received other error: (non-fast-forward)

I write this code:

git fetch origin master:tmp
git rebase tmp
git push origin HEAD:master
git branch -D tmp

And resolved my problem

Upvotes: 144

J D
J D

Reputation: 757

pull is always the right approach but one exception could be when you are trying to convert a none-Git file system to a Github repository. There you would have to force the first commit in.

git init
git add README.md
git add .
git commit -m "first commit"
git remote add origin https://github.com/userName/repoName.git
git push --force origin master

Upvotes: 39

usainzg
usainzg

Reputation: 985

You should use git pull, that´s command do a git fetch and next do the git merge.

If you use a git push origin master --force command, you may have problems in the future.

Upvotes: 43

user1865618
user1865618

Reputation:

Try this git command

git push origin master --force

or short of force -f

git push origin master -f

Upvotes: 26

Related Questions