ローカルで作ったリポジトリをGithubにpushする方法

f:id:kazukitash:20160220152900p:plain

以下のようにローカルのファイルをgitで管理し始めて、GitHubにpushしたくなった時。

$ git init
$ git add .
$ git commit -m 'initial commit'

user名を #{user_name} 、repository名を #{repository_name} とすると、この状態でremote add すると登録はしてくれる。

git remote add origin git@github:#{user_name}/#{repository_name}.git

しかしここで git push しても「そんな repository ないわ」と以下のように返ってくる。

$ git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.

In Git 2.0, Git will default to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Warning: Permanently added the RSA host key for IP address '192.30.252.128' to the list of known hosts.
ERROR: Repository not found.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

GitHub に repository を作っていないのが問題なので、GitHub に sign in して新しい repository を作る。さらに、このままだとローカルの repository の方が古いので git push するとまだ error が出る。

$ git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.

In Git 2.0, Git will default to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

To git@github.com:kazukitash/ps-home.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:kazukitash/ps-home.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

pull して merge すればいい。

$ git pull git@github.com:#{user_name}/#{repository_name}.git master

これで push できる。

ちなみにこのままだとbranch指定なしの git pull ができないので upstream を設定する必要がある。

$ git branch --set-upstream-to=origin/master master 

以上。