第4章 Git

GitHub

リポジトリの作成からプッシュまでの流れ

GitHub上でリポジトリを作成し, ローカルファイルを初めてリモートにプッシュするまでの手順を示す.

git init
git add .
git commit -m "<message>"
git branch -M main
git remote add origin <URL>
git push -u origin master

git initでリポジトリを作成する. git add .はすべてのファイルをステージングする. git commit -m “<message>“でコミットする. git branch -M mainでブランチをmainに変更する. git remote add origin <URL>でリモートリポジトリを追加する. git push -u origin masterでリモートリポジトリにプッシュする.

GitHubを使わずローカルでのみ管理する場合は, commitまでで十分である.

リモートの強制上書き

git push origin mainを試みるとき, リモートとローカルの食い違いが原因で次のようなエラーが出る場合がある:

To <remoteのurl>
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://github.com/is-enaga/mynote.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.

このとき以下のように-fオプションで強制的にプッシュできる.

git push -f origin main