Git Giving "fetch first" error when trying to push

FizzikalGrafiti picture FizzikalGrafiti · Sep 16, 2014 · Viewed 59.3k times · Source

I am having my first introduction to git through a class I am taking. I have a number of files in a directory on my computer and am able to stage and commit them without a problem. However, when I try to push files to my github repository, I keep on getting this message:

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

Can anybody help me out with what this means and how to fix it? I've seen a few people with similar problems online, but I am BRAND NEW to git and not familiar enough with git's command line language yet. I'm a little hesitant to take certain suggestions, as I don't know whether it will solve the problem or make it worse.

Thanks!

Answer

Tuncay Göncüoğlu picture Tuncay Göncüoğlu · Sep 16, 2014

Someone else (or you on some other machine) has pushed a changeset to the remote repository. You, on your local machine dont have those changes yet. So to solve the situation you first have to

git pull

and then

git push

However, you wont be able to git pull while you have changes in your working tree, so before you pull/push, you first have to commit or stash your local changes. This, potentialy, might bring up a merge situation if remote changes overlap with your local ones.