Git リモートレポジトリ作成時に .gitignore ファイルを作成したら、後の Visual Studio Code (VS Code) との同期設定でハマった。
もう二度と .gitignore ファイルその他のファイルは追加しないぞ。
まずはサンプルプログラム用のプロジェクトを用意
プロジェクト名は BotFrameworkSample とした。
最初の GitHub でのレポジトリ作成時に、Add .gitignore で Node を選んだら .gitignore ファイルが自動追加され、そのためにレポジトリへの接続手順ページが表示されなくなってしまった。
いつでも参照できるように empty というプロジェクトを作成し、レポジトリ名だけ置き換えて接続手順を表示させた。
リモートの .gitignore のせいで git push -u origin master に失敗
echo "# empty" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/tombi-aburage/BotFrameworkSample.git
git push -u origin master
o https://github.com/tombi-aburage/BotFrameworkSample.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/tombi-aburage/BotFrameworkSample.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.
ローカルにはないファイルがリモートにあるので更新できなかった。
push の前に pull して統合しろ、とのこと。
コマンドパレット View - Command Palette で Git: Clone したが解消せず。
ローカルのプロジェクトフォルダの中身(.git フォルダ等)を削除して空にしてから、今度は git init だけをまず実行した。
git init
Initialized empty Git repository in E:/Users/tombi/Projects/BotFrameworkSample/.git/
その後で、コマンドパレット View - Command Palette より Git: Clone
を行ったところ、.gitignoreは同期された。その後、
echo "# empty" >> README.md
git add README.md
git commit -m "first commit"
[master (root-commit) 526826f] first commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README.md
ここまではうまくいったのだが、
git push -u origin master
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
となり失敗した。
どうやら、Git: Clone だけでは origin というタグ?が作成されないらしく、
git remote add origin https://github.com/tombi-aburage/BotFrameworkSample.git
を実行する必要があった。しかし追加直後に
git push -u origin master
を実行してみても、やはり同じエラーが再発した。
慌ててしまい、もう一度、コマンドパレット View - Command Palette より Git: Clone を行ったところ、レポジトリが2つに増えてしまった。
・・・下のほうのレポジトリをクローズ。
ソース制御 Source Control より Pull を実行
現在のブランチには追跡情報がない、とのこと。
もう正しい状態が分からん…
すっかり正しい状態が分からなくなってしまったので、根本的にやり直し。
リモートレポジトリから全部作り直し
- GitHub からリモートレポジトリごと削除した。
Setting - Options - Danger Zone から Delet this Repository
正気かどうかを何度も確認された。 - VS Code のワークスペースを閉じて、フォルダ内容を削除
何度も間違えたせいで、BotFrameworkSample フォルダの下にも BotFrameworkSample が入れ子でできてしまっていた。
.git フォルダ、README.md も含む全てのフォルダ・ファイルを削除。 - VS Code のワークスペースを新規作成
ソース制御 Source Control のタブで、初期状態を確認しておく。何もない。
- GitHub にリモートレポジトリを作成
今度は .gitignore を作成しない。いかなるファイルも作成しない。 - レポジトリを同期
GitHub のページに表示されたコマンドを、そのとおり VS Code の Terminal で入力。
echo "# BotFrameworkSample" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/tombi-aburage/BotFrameworkSample.git
git push -u origin master
(略)
PS E:\Users\tombi\Projects\BotFrameworkSample> git push -u origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 254 bytes | 254.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/tombi-aburage/BotFrameworkSample.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
何のトラブルもなく、作成された。
master ブランチは、リモートブランチ master を origin から追跡するように設定されたとのこと。
たぶん追跡を設定する git コマンドをよく理解していれば、リモートレポジトリごと作り直すなどという手段に頼らなくてもよかったのではないかと推測。その勉強は後日。