GitHub関連 エラー対処法メモ
いつもググってしまっている気がするので、メモ書きのために残しておきます。
1. Githubレポジトリ作成
2. アップしたいローカルディレクトリに移動
3. .gitディレクトリ作成
$ git init
これで、カレントディレクトリの下に.gitというディレクトリが作成されて、Gitで使用するファイルが保存されます。
4. カレントディレクトリをaddしてコミット
$ git add . $ git commit -m "first commit"
5. Githubで作成したレポジトリをリモートリポジトリとして登録
git remote add origin [GithubのレポジトリURL]
6. ローカルのディレクトリ、ファイルをリモートへプッシュ
git push -u origin master
<追記 2020/09/15>
pushしようとしたら、こんなエラーが出た。
! [rejected] master -> master (fetch first) error: failed to push some refs to 'https://github.com/ryuryukke/Coursera_Deep_Learning' 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できないみたい。 おそらく、github上でREADME.mdを変更してそのままだったからやな、
・してあげること
リモートリポジトリで変更した内容を、ローカルに反映させる。
・手順
- リモートの「master」ブランチの最新情報を、ローカルの「origin/master」ブランチが受け取る。
git fetch
- ローカルの「origin/master」ブランチから、ローカルの「master」ブランチへ最新情報を持ってくる。
git merge
これで、ローカルのファイル(この場合はREADME.md)が最新の状態に更新された。
そしてpushすると、うまくいった!
このリモートとローカルのブランチの関係を表したこの図がわかりやすかったです。
この図はqiita記事からの引用です。感謝です。
<追記 2020/12/15>
一個前のcommitを取り消したい時
まず、これまでのコミットの履歴をみる
git log
そして
git revert [コミットID]
でok.
git reset は恐いです。
<追記 2021/08/25>
ネストしたレポジトリをGitHubに作りたい
A
┣ B
┗ C
というディレクトリ構成でAをgithubにpushするとBとCがsubmodule化され、開けなくなる。
対処法
git submodule を使う
手順
1. Aに対するリモートレポジトリを作成(https://github.com/A.gitとする)
そして、git init, git remote add origin https://github.com/A.gitを行う
2. BとCをリモートレポジトリに対し、サブモジュールとして追加
BとCそれぞれに対し、
git submodule add https://github.com/A.git B
git submodule add https://github.com/A.git C
を実行。
3. commit & push
git commit -m 'add submodule: B'
git commit -m 'add submodule: C'
git push origin master
これでいける