Skip to content

How to push your new GitHub repo when there’s already a README

2018 August 3
by Tedb0t

I always forget the best way to create and push a new GitHub repo. Often I find myself doing this:

  • Write up some code
  • Decide I want to put it online and do git init in the directory I’m in
  • Go to GitHub and create a repo
  • Look at the “Add a README.md” option and think “Sure, why not”
  • Try to commit and push and get…
git push -u origin master
To https://github.com/username/repo.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/username/repo.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 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.

Whoops. So then I try to pull:

git pull origin master
From https://github.com/username/repo
 * branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories

Weirdly, Googling around doesn’t really help a lot here. One accepted Stack Overflow answer just says “don’t do that, you’ll have to just make a new directory and clone to that.” …fuck that. Turns out it’s as simple as:

git pull --rebase origin master

If you’ve already made commits, you should see:

First, rewinding head to replay your work on top of it...

Then you can just push like normal:

git push origin master

And you’re done!

Related Posts:

  • No Related Posts


Comments are closed.