149

I created a new repository on Github which has only Readme.md file now.

I have a newly created RoR project which I wanted to push to this repository. Following are the commands I gave in my Terminal to execute this along with the error I am getting.

git remote add origin https://github.com/aniruddhabarapatre/learn-rails.git

After which I entered my username and password

git push -u origin master

Error ---

To https://github.com/aniruddhabarapatre/learn-rails.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/aniruddhabarapatre/learn-rails.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 merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

This is my first time pushing my code to a Github repository and I'm lost with the errors. I searched few other questions that are asked here, but none of them had issues first time.

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
Aniruddha
  • 3,157
  • 4
  • 32
  • 48
  • 1
    possible duplicate of [Github "Updates were rejected because the remote contains work that you do not have"](http://stackoverflow.com/questions/18328800/github-updates-were-rejected-because-the-remote-contains-work-that-you-do-not-h) – Colin T Bowers May 18 '15 at 04:26

14 Answers14

283

If this is your first push, then you might not care about the history on the remote. You could then do a "force push" to skip checks that git does to prevent you from overwriting any existing, or differing, work on remote. Use with extreme care!

just change the

git push **-u** origin master

change it like this!

git push -f origin master
stolsvik
  • 5,253
  • 7
  • 43
  • 52
Van Mart
  • 3,002
  • 1
  • 10
  • 5
  • 5
    This deleted my readme file that I put up on github first. Now it looks like I have to make it over again. – jack blank Jan 19 '17 at 15:33
  • 3
    you may just add a warning that -f forces the push and could mess with the code timeline – ericksho Jan 22 '18 at 20:06
  • This is a bad way, it caused me a problem, it deleted the readme, which was not created by me, now I have to call the maintainer tell him that I am stupid, sorry create the read me again. :( – Mohammad Elsayed Feb 04 '20 at 15:48
169

When you created your repository on GitHub, you created a README.md, which is a new commit.

Your local repository doesn't know about this commit yet. Hence:

Updates were rejected because the remote contains work that you do not have locally.

You may want to find to follow this advice:

You may want to first merge the remote changes (e.g., 'git pull') before pushing again.

That is:

git pull
# Fix any merge conflicts, if you have a `README.md` locally
git push -u origin master
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • 3
    What I'm doing is git pull origin master first then push again. – Bagusflyer Jul 08 '14 at 16:19
  • 16
    in some cases you might get a `fatal: refusing to merge unrelated histories` in such a case, you submit `git pull --allow-unrelated-histories origin master` and then you push as per the answer above – TheLebDev Sep 01 '18 at 10:42
  • 1
    Doesnt work for me. remote: Push rejected. remote: remote: refs/heads/master: ...: expected committer name xxx but found yyy. I do a git config --global user.email yyy and it still doesnt recognize. Can't force anything.!!! – Baruch Atta Apr 01 '19 at 17:48
63

⚡️ EASY: All you need is a forced push. Because you might have created readme.md file on Github and you haven't pulled it yet.

git push -f origin master

And here's a GIF.

git push -f origin master

⚠️ BEWARE: Using force can change the history for other folks on the same project. Basically, if you don't care about a file being deleted for everyone, just go ahead. Especially if you're the only dev on the project.

Ivan Aracki
  • 4,861
  • 11
  • 59
  • 73
Ahmad Awais
  • 33,440
  • 5
  • 74
  • 56
  • 1
    Note this approach changes history that other users of this repository have already fetched. `git push -f origin master` is not a great habit to form. – johnsyweb Dec 27 '17 at 04:13
  • 1
    This solution may solve pushing commit but it also remove all of past commits!. – twenk11k Feb 21 '19 at 14:02
  • 2
    Beware Github noobs... If you have created a Readme.md via GitHub, this command WILL REMOVE IT as it pushes your new changes. – daCoda Feb 28 '19 at 03:04
  • That is mostly why people land here. `-f` tag removes files and should be used with care. – Ahmad Awais Mar 01 '19 at 05:00
34

Issue a forced push with the command:

git push -f origin master
Melebius
  • 6,183
  • 4
  • 39
  • 52
Samarth Shah
  • 878
  • 8
  • 14
8

Assuming that you added the Readme.md file through the interface provided by github, the readme is not yet in your local folder. Hence, when you try to push to the remote repo, you get an error, because your local repo is lacking the readme file - it's "behind the times", so to speak. Hence, as is suggested in the error message, try "git pull" first. This will pull the readme from the remote repository and merge it with your local directory. After that, you should have no problem pushing to the remote repo (the commands you posted look valid to me).

Quercus
  • 363
  • 2
  • 8
8

This is happen when you try to push initially.Because in your GitHub repo have readMe.md or any other new thing which is not in your local repo. First you have to merge unrelated history of your github repo.To do that

git pull origin master --allow-unrelated-histories

then you can get the other files from repo(readMe.md or any)using this

git pull origin master

After that

git push -u origin master

Now you successfully push your all the changes into Github repo.I'm not expert in git but every time these step work for me.

Chathuranga
  • 316
  • 4
  • 12
5

Considering if you haven't committed your changes in a while, maybe doing this will work for you.

git add files
git commit -m "Your Commit"
git push -u origin master

That worked for me, hopefully it does for you too.

Melebius
  • 6,183
  • 4
  • 39
  • 52
Rob
  • 338
  • 3
  • 5
4

This error occurs when you push the data from your local directory to your remote git repository by following git command: git push -u origin master

As local directory and git remote directory's files conflicted.

Solution :

After committing all files to staging follow below steps.

  1. Fetch the files from the remote repository as its conflict with the local working directory.

    • git pull <remoter-url> <branch-name>
  2. Commit the changes again.

    • git add -A
    • git commit -m ‘<comment>'
  3. After committed merge files with both directory you can use

    • git push -u origin master

This will fix the issue. Thanks.

Ravi
  • 41
  • 2
3

if you use the git for mac in GUI you can chose Respository->Pull or the "comm+shift+p" to "git pull" first, then publish the source.

1

I had a similar problem... I resolved it like this (i'm not an git expert so i don't know if it is a right solution, but it worked for me):

git pull origin master --allow-unrelated-histories
git merge origin origin/master
git rm README.md
git commit -m 'removed readme.md'
git push origin master
Marco Caggiano
  • 308
  • 2
  • 8
0

I struggled with this error for more than an hour! Below is what helped me resolve it. All this while my working directory was the repo i had cloned on my system.

If you are doing adding files to your existing repository** 1. I pulled everything which I had added to my repository to my GitHub folder:

git pull


Output was- some readme file file1 file2

  1. I copied (drag and drop) my new files (the files which I wanted to push) to my cloned repository (GitHub repo). When you will ls this repo you should see your old and new files.

eg. some readme file file1 file2 newfile1 newfile2

  1. git add "newfile1" "newfile2"

  2. [optional] git status this will assure you if the files you want to add are staged properly or not output was


On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD ..." to unstage)

    new file:   newfile1
    new file:   newfile2

5.git commit -m "whatever description you want to give" 6.git push

And all my new files along with the older ones were seen in my repo.

0

A simpler answer is to manually upload the README.MD file from your computer to GitHub. Worked very well for me.

Bogdan Doicin
  • 2,342
  • 5
  • 25
  • 34
0

I use the Branches options, and then right click on the "remote/origin" folder and then click on "delete branches from remote", see the image below:

the image

hestellezg
  • 3,309
  • 3
  • 33
  • 37
0

I got this error on Azure Git, and this solves the problem:

git fetch
git pull
git push --no-verify
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
canmustu
  • 2,304
  • 4
  • 21
  • 34