そういう、モデルなんです。

ビジネスモデル、3Dモデル、設計図、模型などの現状と動向を考察、関連書籍の紹介

Git リモートレポジトリ作成時に .gitignore ファイルを作成したら、後の設定でハマった。

Git リモートレポジトリ作成時に .gitignore ファイルを作成したら、後の Visual Studio Code (VS Code) との同期設定でハマった。

f:id:tombi-aburage:20190524125322p:plain

.gitignore が邪魔で push できず…

もう二度と .gitignore ファイルその他のファイルは追加しないぞ。

まずはサンプルプログラム用のプロジェクトを用意

プロジェクト名は BotFrameworkSample とした。 

tombi-aburage.hatenablog.jp

  1. GItHub で新しいプロジェクト(レポジトリ)を作成

  2. VS Code で新しいプロジェクトを作成

  3. レポジトリを同期

最初の 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

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

f:id:tombi-aburage:20190518101205p:plain

コマンドパレットで 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.

となり失敗した。

origin が見当たらない…

どうやら、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つに増えてしまった。

f:id:tombi-aburage:20190518101205p:plain

コマンドパレットで Git: Clone

f:id:tombi-aburage:20190518101950p:plain

何度も失敗を繰り返した結果、おかしなことに…
pull と clone を勘違いした!

・・・下のほうのレポジトリをクローズ。

ソース制御 Source Control より Pull を実行

f:id:tombi-aburage:20190518102554p:plain

Source Control - Pull

現在のブランチには追跡情報がない、とのこと。

f:id:tombi-aburage:20190518102356p:plain

Git: There is no tracking information for the current branch

もう正しい状態が分からん…

すっかり正しい状態が分からなくなってしまったので、根本的にやり直し。

リモートレポジトリから全部作り直し

  1. GitHub からリモートレポジトリごと削除した。
    Setting - Options - Danger Zone から Delet this Repository
    正気かどうかを何度も確認された。
  2.  VS Codeワークスペースを閉じて、フォルダ内容を削除
    何度も間違えたせいで、BotFrameworkSample フォルダの下にも BotFrameworkSample が入れ子でできてしまっていた。
    .git フォルダ、README.md も含む全てのフォルダ・ファイルを削除。
  3. VS Codeワークスペースを新規作成
    ソース制御 Source Control のタブで、初期状態を確認しておく。何もない。

    f:id:tombi-aburage:20190518104609p:plain

  4. GitHub にリモートレポジトリを作成
    今度は .gitignore を作成しない。いかなるファイルも作成しない。
  5. レポジトリを同期
    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 コマンドをよく理解していれば、リモートレポジトリごと作り直すなどという手段に頼らなくてもよかったのではないかと推測。その勉強は後日。