ローカルファイルをGithubにアップロードする手順
【手順1】 Githubにリモートリポジトリを作成
この段階ではInitialize this repository with: はスルー
【手順2】 ローカルリポジトリ を作成
①アップロードしたいフォルダに移動
コマンド-->cd フォルダのパス
Macならフォルダをそのままドラッグ&ドロップすると自動でパスが入力できる
②ローカルリポジトリを作成
コマンド-->git init
Initialized empty Git repository・・・と出ればOK
③ステージング
コマンド-->git add .
対象フォルダの全てのファイルやフォルダを、アップロードの対象にする。
ターミナルには何も表示されない
④ コミット(保存)
コマンド-->git commit -m "コミット名"
コミット名はメモ書きみたいなもので、ダブルクオーテーション必須
【手順3】 ローカルとリモートを紐付け
①git remote add origin url名
コマンド-->git remote add origin url名
url名-->新規リポジトリを作成したときの画面に出てくるurl
実行してもターミナルには何も出ない
ローカルとリモートを紐付けるためのコマンドなので2回目のpushからは入力しない
②push(アップロード)
コマンド-->git push origin master
これでGithubに反映される
2回目以降のpush手順
①アップロードしたいフォルダに移動
コマンド-->cd フォルダのパス
②ステージング
コマンド-->git add -A
※git add オプション
git add .
-->現在のディレクトリ以下の、変更があったすべてのファイル(変更されたファイル、削除されたファイル、新規ファイル)がステージングされる
git add -A
-->変更があったすべてのファイル(変更されたファイル、削除されたファイル、新規ファイル)がステージングされる
git add -u
-->バージョン管理されていて、変更されたファイル、削除されたファイルがステージングされる。新規ファイルはステージングされない。
③コミット
コマンド-->git commit -m "コミット名"
④フェッチ
コマンド-->git fetch origin
リモートリポジトリの最新の履歴を取得
⑤マージ
コマンド-->git merge --allow-unrelated-histories origin/master
--allow-unrelated-historiesを付けないと、以下のエラーが出る場合がある
fatal: refusing to merge unrelated histories
⑥プッシュ
コマンド-->git push origin master
*④⑤をスキップした場合、以下のようなエラーが出る
! [rejected] master -> master (fetch first)
error: failed to push some refs to '.....'
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.
参考
初めてのpush時に出たエラーの対処法まとめ
git add -u と git add -A と git add . の違い