Git のローカルリポジトリで開発していたプロジェクトをGitHubで公開する際に余計なことをして手間取った点についての備忘録。
GitHubで公開用プロジェクトを作成する際に空のリポジトリにすれば、作成後に示されるページの内容に従って次のコマンドをローカルリポジトリがあるディレクトリで実行すれば良い。
Owner/RepositoryName
の部分は自分のリポジトリに合わせる必要がある。以下同様。
1 2 |
git remote add origin https://github.com/Owner/RepositoryName.git git push -u origin master |
しかし、プロジェクトを作成する際に次のところでファイルを追加するようにすると上記の操作だけではうまくいかない。
上記の
git push -u origin master
を実行すると次のようなエラーとなる。
1 2 3 4 5 6 7 |
! [rejected] master -> master (fetch first) error: failed to push some refs to 'https://github.com/Owner/RepositoryName.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. |
そこで
git pull origin master
を実行してみると次のようなメッセージが出て正常にpullできない。
1 2 3 |
From https://github.com/Owner/RepositoryName * branch master -> FETCH_HEAD fatal: refusing to merge unrelated histories |
これを解消するには
git merge --allow-unrelated-histories origin/master
を実行する。
この後で
git push -u origin master
を実行することで GitHubへのpushが完了する。
GitHubで公開用のリポジトリを作成する際には必ず空にしておくことが重要である。そうしないと手間が増える。