Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

! [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'
like image 243
Symfony Avatar asked Feb 10 '15 10:02

Symfony


People also ask

What does fetch first mean?

It means that someone has pushed work to the remote repository, to merge it with your work you can run git pull --rebase then push your combined work back to the remote repository.

What is fetch in git?

Git fetch summary In review, git fetch is a primary command used to download contents from a remote repository. git fetch is used in conjunction with git remote , git branch , git checkout , and git reset to update a local repository to the state of a remote.

What does rejected non fast forward mean?

This error is faced when git cannot commit your changes to the remote repository. This may happen because your commit was lost or if someone else is trying to push to the same branch as you. This is the error you face.

What does git fetch origin master do?

The git fetch command downloads objects to the local machine without overwriting existing local code in the current branch. The command pulls a record of remote repository changes, allowing insight into progress history before adjustments. Read on to learn how to use the git fetch command through hands-on examples.


3 Answers

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.

like image 86
linuxbandit Avatar answered Nov 16 '22 00:11

linuxbandit


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

like image 37
Aurelio A Avatar answered Nov 15 '22 23:11

Aurelio A


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.

like image 22
usainzg Avatar answered Nov 16 '22 00:11

usainzg