エラーが起きた状況
あるリモートリポジトリ(フォルダ名を'system'とする)をフォークして、ローカルにクローンした。
systemの中にディレクトリsystem/webappを作成し、そこでアプリケーションの開発作業を行なっていた。
これが全ての元凶なのだが、system/webapp内でgitを作成してしまう。
作業終了後、リモートリポジトリにpushするが、当然うまく行かない・・・
cd system/webapp
git init
git add -A
git commit -m "..."
git push
To https://github.com/user/system
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/user/system'
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する必要があることに気づき、下記のように対応した。
cd system
git add -A
warning: adding embedded git repository: /webapp
hint: You've added another git repository inside your current repository.
hint: Clones of the outer repository will not contain the contents of
hint: the embedded repository and will not know how to obtain it.
hint: If you meant to add a submodule, use:
hint:
hint: git submodule add <url> /webapp
hint:
hint: If you added this path by mistake, you can remove it from the
hint: index with:
hint:
hint: git rm --cached /webapp
hint:
hint: See "git help submodule" for more information.
addしたら警告が表示されたが・・・push!
git commit -m "..."
git push
リモートリポジトリを確認すると、webappフォルダはアップロードされているが、フォルダ以下のファイルが無い・・・
さて、どうしよう。
解決策
下記サイトを参考にして対処しました。行なったことは次の2点。
1.system/webapp内のローカルリポジトリを削除し、pushする
rm -rf webapp/.git
git add webapp/*
fatal: Pathspec 'webapp/hoge' is in submodule 'webapp'
webappがサブモジュールとして登録されてしまっており、webapp/.gitを削除しても、webapp以下のファイルをアップロードできないようだ。
2.webappディレクトリをgitの管理対象から一度削除する
git rm --cached webapp
git add -A
git commit -m "..."
git push
webapp以下のファイルがついにローカルリポジトリ、リモートリポジトリに反映され、無事解決!