目的
GitHubに入門して、初めてlocal, remote repositoryを作成して投稿するまでの流れを備忘録を兼ねて書きます。
Remote repositoryを作成する
まずは、GitHubのページでrepositoryを作成します。GitHubにアカウントを持っていない人は登録しましょう。
- 画面右上のプラスボタンをクリックして、「New repository」をクリックします。
- Repository Nameを記入します。
- Public, Privateを選択する部分がありますが、無料版ではPublicしか選べません。
- 更にその下に、ReadMeファイルを作るか、などありますがチェックはしないでおきます。最初、ReadMeファイルを作成したために、エラーが出てしまい苦労しました。
このような画面が出ます。赤枠で囲んだ部分はrepositoryのアドレスで、このあとlocal repositoryとつなげるときに必要になりますのでコピーしておきます。
Local repositoryを作成する
次に、パソコン上にlocal repositoryを作成します。コマンドライン上で、
$ mkdir git_test #新しいフォルダを作成
$ cd git_test
$ git init #local repositoryを作成
これでlocal repositoryが作成されました。この中にファイルを作成していきます。ここでは例としてtest.txt
という簡単なテキストファイルを作りました。これを追加するには
$ git add test.txt
$ git commit -m "This is a test file." #コメント
などとします。
GitHub上にPushする
では、local repositoryをGitHubのRemote repositoryにPushします。ここで、先ほど確認したRemote repositoryのアドレスを使います。
$ git remote add origin https://"アドレス"
$ git push -u origin master
これでPush完了です。
Remote repositoryを作成するときに、ReadMeファイルを作ってしまうと、Pushするときに
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/ /test.git'
ヒント: Updates were rejected because the remote contains work that you do
ヒント: not have locally. This is usually caused by another repository pushing
ヒント: to the same ref. You may want to first integrate the remote changes
ヒント: (e.g., 'git pull ...') before pushing again.
ヒント: See the 'Note about fast-forwards' in 'git push --help' for details.
なるエラーが出てきて、Pushできません。理由は謎です