Issue pushing to a GitHub repository created with “Initialize repo with a README” option

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:

  • Github: How do I pull unmerged upstream pull requests in to my fork?
  • git rebase and deleted rebasing branches causing “Interactive rebase already started” error message
  • How to find if a branch is a locally tracked branch or user created local branch?
  • Symfony 1.4 best practice production & development configuration
  • No such file or directory - git ls-files — WINDOWS
  • Does pushing to the current branch of a remote repository update the remote's working directory?
  • 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?

    1. Why could I not pull when I tried to fix this error?

    2. 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?

    3. How do I create a GitHub repository with an initialized README and connect it to a local repo without causing these errors?

  • Find Git commits that contain multiple specific commits
  • Error while installing RVM on ubuntu 11.04. Seems to be git clone's error
  • Is there a better database than Git (with serializable, immutable, versioned trees)?
  • How can I amend a previous git commit
  • Redoing Commit History in GIT Without Rebase
  • Git: Test combination of two feature branches
  • 2 Solutions collect form web for “Issue pushing to a GitHub repository created with “Initialize repo with a README” option”

    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.

    Let’s say you have a local repository:

    $ git log --oneline
    8e7e8d4 hello.txt
    

    The local repository has a single file:

    $ ls
    hello.txt
    

    Now you create a new repository via the GitHub web interface and initialize it with a README file. At this point, your two repositories have divergent histories. You can add the remote repository to your local repository:

    $ git remote add origin git@github.com:larsks/samplerepo.git
    

    But attempting to pull from this repository will yield an error:

    $ git pull
    warning: no common commits
    remote: Counting objects: 3, done.
    remote: Total 3 (delta 0), reused 0 (delta 0)
    Unpacking objects: 100% (3/3), done.
    From github.com:larsks/samplerepo
     * [new branch]      master     -> origin/master
    There is no tracking information for the current branch.
    Please specify which branch you want to merge with.
    See git-pull(1) for details
    

    The important part of this error message is:

    There is no tracking information for the current branch.
    Please specify which branch you want to merge with.
    

    This is git telling you that while your current branch in your local repository is not associated with any branch in the remote repository, so it doesn’t know what to do. You can provide it with a remote branch name explicitly:

    $ git pull origin master
    

    This will create a merge commit (and will probably prompt you for a commit message). Once the commit is complete, you can see that the local history now contains both our local commit as well as the commits from the GitHub repository:

    $ git log --oneline
    7f1231a Merge branch 'master' of github.com:larsks/samplerepo
    5f0d62e Initial commit
    8e7e8d4 hello.txt
    

    And our working directory now has a merged set of files:

    $ ls
    hello.txt  README.md
    

    And we can push this to our remote repository. Just typing git push will result in an error similar to what we saw earlier:

    $ git push
    fatal: The current branch master has no upstream branch.
    To push the current branch and set the remote as upstream, use
    
        git push --set-upstream origin master
    

    So:

    $ git push --set-upstream origin master
    Counting objects: 6, done.
    Delta compression using up to 8 threads.
    Compressing objects: 100% (3/3), done.
    Writing objects: 100% (5/5), 543 bytes | 0 bytes/s, done.
    Total 5 (delta 0), reused 0 (delta 0)
    To git@github.com:larsks/samplerepo.git
       5f0d62e..7f1231a  master -> master
    Branch master set up to track remote branch master from origin.
    

    And now we’re all in sync.

    Update: Regarding your questions about --set-upstream:

    When you checkout a local branch that matches the name of a remote branch, git will set up the upstream association for you automatically. For example, if the remote repository in this example also had a branch “development”, and I did this after adding the remote:

    $ git checkout development
    

    I would see:

    Branch development set up to track remote branch development from origin.
    Switched to a new branch 'development'
    

    On the other hand, if you already have a branch checked out when you add the remote, as in the first part of this example, you need to use --set-upstream to tell git that you want your local branch to track the remote branch.

    As an aside, note that there’s no requirement that your local branch match the name of a remote branch. You are free to do something like:

    git checkout master
    git push --set-upstream origin patches
    

    So that from this point on, git push on your master branch will push to the remote patches branch.

    Git Baby is a git and github fan, let's start git clone.