19

I was setting up a new repository on GitHub and was trying to push to it but Git kept giving me an error saying:

error: failed to push some refs to...

I eventually tried pulling first and then pushing and that worked. But why?

1
  • Because somebody has already pushed some new commits to the remote repo. Now git assumes that you need to fetch those new changes, merge them with your changes and then push the resulted changes back Commented Nov 1, 2015 at 2:37

4 Answers 4

29

You mentioned that you were creating a new repository.

While the answer's and comments are also true, it is likely you're the only person interacting with the repository. You were required to pull because you initialized the repository with a README on GitHub (this is likely the tutorial you followed).

If you did not initialize the repository with a README, meaning GitHub didn't make a first commit of 'README.md', you would have a completely empty repository you can directly push to.

The reason why GitHub has that option is most likely to assist users who are starting a new project (such as yourself) to very easily get going after setting up a repository on GitHub by doing a pull/clone and having that initial commit in, allowing you to quickly add new files and push.

Additionally, by initializing a repository with a README, you'll have a master branch ready to clone and start tracking files. While on a completely empty repository you will receive notifications from Git like:

warning: You appear to have cloned an empty repository.

Without initializing, you will also later have to push your first commits the first time explicitly to master with git push origin master, as Git will politely tell you:

No refs in common and none specified; doing nothing.
Perhaps you should specify a branch such as 'master'.
Everything up-to-date

To summarize, it was that first commit (see your commits and you'll see the first README commit) that prevented you from pushing without pulling as your local repository is not in sync with the repository on GitHub.

2
  • 1
    Would it be feasible for him to rewrite history to erase that readme file and then not have to pull?
    – boot4life
    Commented Nov 1, 2015 at 12:23
  • @boot4life well, more like 'overwrite' than rewrite, assuming one hasn't pulled and the readme file is only on remote (i.e GitHub) then one can force push, see stackoverflow.com/a/12610763/4512948 but also see why this isn't good if you're collaborating: developer.atlassian.com/blog/2015/04/force-with-lease TL;DR you basically ignore whatever there was and overwrite it all with what you have, which is fine if you know for sure there isn't anything you want there. Else you're changing history for everyone. Commented Nov 1, 2015 at 15:19
6

That's so that any parallel edits made by someone else and then checked in are brought into your development environment for merging and testing. In this way, the number of parallel, non-integrated changes is kept to a manageable minimum

3

In your case, GitHub has automatically created the first commit (often README.md and LICENCE files) and in order to push your commits, you must first pull (fetch and merge) GitHub's commit.

2

When you pull, Git will fetch commits on origin and will try to fast-forward your local commits on top of them, doing the merge. After that you can push in this way, and you will not generate conflicts with other updates.

1
  • 2
    Pull does a merge - it's not a fast forward. Fast forward is what happens when you push.
    – bdsl
    Commented Nov 1, 2015 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.