22

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!

FizzikalGrafiti
  • 329
  • 1
  • 2
  • 7
  • 4
    The solution is in the error message itself – Zac Hallett Sep 16 '14 at 06:54
  • There are some difference on the master on github and first you need to fetch them; so just do a `git fetch` first then do the `git push origin master` – sop Sep 16 '14 at 06:55
  • 1
    @sop Just a git fetch won't suffice, he also has to merge/rebase the changes. – Sascha Wolf Sep 16 '14 at 06:55
  • 1
    Did you run `git commit --amend` after pushing? That may explain the "problem". – jub0bs Sep 16 '14 at 07:40
  • Also, it's unclear whether you're the only one who uses the remote repository residing at `https://github.com/BigMeanCat/CMDA` or not. Do other students in your class use it too or are you the only one? – jub0bs Sep 16 '14 at 08:59
  • Jubobs: I am working with a team of other students, but I have not given them access to my repository yet. And no, I haven't run git commit or any other commands. The error message says "the remote contains work that you do not have locally." Does this mean that any files existing in my remote repository on github must also exist in the local folder that I am trying to push from? I am asking because we created a README file in class that is not in my local directory. – FizzikalGrafiti Sep 16 '14 at 18:58
  • I finally figured out what was wrong. Since I was originally having some issues with installing the git gui for Ubuntu, I had tried taking a shortcut with two assignments by using "Create a new file here" and copying the code directly into the new file in my github repository. Obviously, it didn't like this and wouldn't let me pull at first, so I deleted the two files and left the README file and was able to pull that file. After that, I could push everything I needed to. Still trying to puzzle out why it didn't like the two hard-coded files, but thanks for your help, everyone! – FizzikalGrafiti Sep 16 '14 at 22:57

11 Answers11

27

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.

Tuncay Göncüoğlu
  • 1,699
  • 17
  • 21
  • Thanks! I figured out my problem and explained it in some of the comments above, but git pull and git push was exactly what I needed. – FizzikalGrafiti Sep 16 '14 at 23:01
  • I come to this post every time I get this message and this solution never works?? Ah it's because I add the license key every time. See "You get this error when your Github repository has a file which is not in your local computer." https://www.edureka.co/community/63787/rejected-master-master-fetch-first-error-failed-github-abc70 – Brian Wiley Jul 22 '21 at 03:57
27

I have been taken same error, then I solved this topic with used "--force" command. Briefly, write this command;

git push origin master --force

Attention: Probably, you tried to do push your codes over and over again before , that's why you took this error.My solution overwrite forcefully with your changeset. By this method your repository may cause mismatch other people's. But If you work alone (not like a group working), so you can use easily "--force" as I mentioned above.

Purgoufr
  • 761
  • 14
  • 22
9

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.

Emil Davtyan
  • 13,808
  • 5
  • 44
  • 66
  • 1
    The OP is new to Git. I wouldn't recommend such an "advanced" command as `git pull --rebase` to a beginner. – jub0bs Sep 16 '14 at 06:56
  • 1
    @Jubobs I think this is the easiest way to deal with a potential merge. – Emil Davtyan Sep 16 '14 at 07:01
  • Perhaps, but it may also confuse the OP, at such an early stage of his/her Git learning. Anyway, I think we're missing critical information, here. We should wait for the OP to give us more details as to what s/he did. – jub0bs Sep 16 '14 at 09:02
  • Thanks. git pull did the trick once I made some changes to my remote repository. I'm figuring it out little by little, but there's a lot of jargon I need to pick up before I can figure what I'm moving around and where I'm sending it. – FizzikalGrafiti Sep 16 '14 at 23:03
9

You can solve the problem by the command.

git push origin master --force
Rubel Hossain
  • 2,503
  • 2
  • 22
  • 23
  • 1
    This overrides `git push` restriction. Not recommended for teamwork. From git documentation: If somebody else built on top of your original history while you are rebasing, the tip of the branch at the remote may advance with her commit, and blindly pushing with --force will **lose her work**. – Casey Oct 04 '17 at 09:54
4

! [rejected] master -> master (fetch first)'
Use: $ git pull
then: $ git push
Alternate solution: $ git push --force origin master but this will result in removal of files in your github repository so better prefer the first one it will pull the file first to your local repository and then you can push your work.

  • Can you please explain this answer in further detail. For example, what is the purpose & result of the first two operations - why does this solve the problem? – Kingsley Sep 21 '18 at 00:04
2

@tunacy said it well.

someone has committed repository , so you just need to do

git pull

and

git push 

that's all you can also use

git merge 

for the merge

Sh Huzaifa
  • 127
  • 7
2

git pull is a good option

But also: git push origin master --force command will eventually help if the problem persists

Kurian Benoy
  • 849
  • 6
  • 12
1

I Haved this exactly same problem. I asked a friend for a help and he just copied the paste to the main repository and wrote the comand below: git remote show origin git add . git commit -m "including project2 in the main repository" git push -u origin master

In the end I got two pastes in the main repository of two diferent projects.

0

When you start working with git, you may take a look a bit into documentation: http://git-scm.com/doc. From your description I understand, you have existing repository with the files which are shared with the other classmates. Therefore you need to first pull the whole project (with all the files). Having up-to-date version of the project on your local repository, you may push commit changes to the repository.

Mateusz
  • 51
  • 5
  • Thanks. I had been looking through that manual earlier, but I'm still figuring out what's going on. I am working with a team of other students, but I have not given them permissions to work with my files or push to my repository. When I pull, am I making a pull request at the remote repository or from the command line from within the local directory that contains my files (my local repository, I assume)? Does the local repository have to have the same name as the local repository? – FizzikalGrafiti Sep 16 '14 at 19:13
  • The **local** repository shall have the same name (i.e. folder name) and the structure of files must be the same as it is on the **remote** repository. The git pull command will do the update of you local repository so it is exactly the same as the remote repository. – Mateusz Sep 17 '14 at 11:37
0

I encountered this error when I opened terminal/GitBash in the wrong directory. Actually outside the repository folder. Check whether you are in correct location.

JEJC_JACALK
  • 409
  • 1
  • 5
  • 13
0

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)

dippas
  • 58,591
  • 15
  • 114
  • 126
Bhavdeep Kaur
  • 89
  • 1
  • 2