Issue pushing to a GitHub repository created with "Initialize repo with a README" option
git, github
Solution
The errors are saying that your repo has changes that you don't have yet, because you added the `README` to the remote one when you set it up. If you've already got local changes or a local repo, you need to initialise an empty repository on Github, and then you can push. You'll have to add the remote though, something like `git remote add https://github.com/username/repo.git`.
Downloading the README manually and adding and committing will probably produce a different commit ID, and put it at a different point in the commit history, which is why it's not detected as the same one.
1) Why does it not work to initialize a remote repository on github with a README, and then try to connect the github repo with a previously existing existing local repo?
When Github adds the README it commits it, and then this is the first commit. If you have a local repo, the first commit locally will be different, so they won't match up.
2) Why could I not pull when I tried to fix this error?
Probably because of the above, or the `remote` reference didn't add in properly, depending on how you added it.
Generally, if you're creating locally first, you would go:
# Set up the Git repo locally, with no commits in it.
git init
# Add a new file.
git add file1
# Commit the change.
git commit
# Tell Git where your remote is.
git remote add origin https://github.com/user/repo.git
# Push the default 'master' branch to the above remote called 'origin'.
git push origin master
Or if it already exists on Github or a different remote server:
# Download the existing repo, with all of the history.
git clone https://bitbucket.org/user/repo.git
# Add a new file or modified file.
git add file1
# Commit the change.
git commit
# Push to the remote that you downloaded from with clone on branch master.
git push origin master
3) Why could I still not push and initialize the connection to my github remote after I added the README from github to my local repository manually?
That's not how the changes work with Git; they're a big list of sequential commits in a chain. Each commit has one or two parent commits, and the commit IDs aren't sequential either.
See the Git website for some diagrams on the branching and commits here: http://git-scm.com/book/en/Git-Branching-What-a-Branch-Is
4) How do I create a github repo with an initialized README and connect it to a local repo WITHOUT causing the errors below?
If you have an existing local repo you shouldn't create one with the initialised README. If it's blank on Github when you create it, you can push up your existing repository with no errors. If it has the README, you have to `git clone` the Github repo, and then add your changes to that folder, commit the changes, and then push. Adding the README is for when you have a new project and you're creating the Github repo first, and then you clone the project and start working in that location. If you have an existing repository locally don't use that option.
Problem
I tried to create a GitHub repository using the instructions in the GitHub documentation, except instead of making a `README` locally, I initialized my GitHub repository with the `README` option. After trying to push, though, I get this error I don't fully understand: ``` kirby:cs61as_SCIP_schython \**user**$ git push origin master https://github.com/chris-marie/cs61as_SICP_schython.git ! [rejected] master -> master (fetch first) error: failed to push some refs to 'https://github.com/chris-marie/cs61as_SICP_schython.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. ``` I couldn't pull the repository either, so I tried manually downloading, adding, and committing the `README` file I had created virtually and tried to push again, and got a new error: ``` kirby:cs61as_SCIP_schython \**user**$ git push origin master https://github.com/chris-marie/cs61as_SICP_schython.git ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'https://github.com/chris-marie/cs61as_SICP_schython.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Merge the remote changes (e.g. 'git pull') hint: before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. ``` This leaves me with four questions: 1.. Why does it not work to initialize a remote repository on GitHub with a `README`, and then try to connect the GitHub repository with a previously existing existing local repository? Why could I not pull when I tried to fix this error? Why could I still not push and initialize the connection to my GitHub remote after I added the `README` from GitHub to my local repository manually? How do I create a GitHub repository with an initialized `README` and connect it to a local repo without causing these errors?