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する

参考: https://wonderful-tereshkova-38f91d.netlify.app/GitHub%E3%81%B8%E3%81%AEpush%E3%81%8C%E3%80%8Cfetch%20first%E3%80%8D%E3%81%A8%E8%A1%A8%E7%A4%BA%E3%81%95%E3%82%8C%E3%81%A6rejected%E3%81%A8%E3%81%AA%E3%81%A3%E3%81%9F%E3%81%A8%E3%81%8D%E3%81%AE%E5%AF%BE%E5%87%A6/

これで手元のフォルダをgithub上の任意のリポジトリへpush可能