Git でGithubにフォルダをpushする方法
参考記事
https://techacademy.jp/magazine/6235#sec4
https://backlog.com/ja/git-tutorial/reference/basic/
記事作成の背景
githubにLaravelのフォルダをアップロードしようとしたらフォルダのファイル数が100を超えていて、アップロード出来なかった。
↓
ターミナルでpushしてアップしてみよう!
やり方
git hubで新規リポジトリ作成
作成したリポジトリのURLを取得
pushしたいプロジェクトフォルダをVSCodeで開く
VSCodeのエディタ内のターミナルを開く(そうするとディレクトリが最初から該当フォルダの階層から始まる)
ここまで出来たらターミナル操作
・git init
・git add .
※addの後を「.」とすることで現在いる階層のファイルを全てaddすることが可能。
1ファイルずつ指定するときは
git add hoge.html
のように指定すればOK
・git commit -m "add new file"
アドの次はコミット
※""の中に任意のコミットメッセージを入れる
・git remote add origin https://github.com//awesome.git
※fatal: remote origin already exists.
上記のエラーが出た時の対処法(過去のgit remote addの履歴が残っていた時はこの対応をする)
$ git remote rm origin
参考:https://qiita.com/yu-ki0718/items/3c8aae2c81ca3f82f522
https以降は最初githubで作成したリポジトリのURLを貼り付ける
・git push origin master
*githubで新規リポジトリにREADEMEを作っているとここでコンフリクトが起きる。
エラーはこんな感じ
! [rejected] master -> master (fetch first)
error: failed to push some refs to ‘https://github.com/WakkyFree/binarycutter’
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushin
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.
原因は手元のエディタにはREADE.MEがないから。
そこでその場合は以下の対応をする*
↓
git fetch
↓
git add READEME.md(READEME.mdの部分はローカルで存在していなくて、リモートにあるファイルを指定する)
↓
git commit -m "2nd commit"
↓
git push origin master
リモートリポジトリにpushする
これで手元のフォルダをgithub上の任意のリポジトリへpush可能