node_modules を Git 管理から外したい。この手順への指摘として正しいものは?
echo "node_modules/" >> .gitignoregit add .gitignoregit commit -m "Ignore node_modules"git add -f node_modules/git commit -m "Add deps"正答
- 4 行目の add -f は .gitignore を無視して強制追加する
- 1 行目は > にしないと .gitignore に書き込まれない
- 2 行目で .gitignore 自体を add すると無視設定が効かない
- 3 行目の後に push しないと .gitignore が効かない
解説
git add -f(--force)は .gitignore で無視されているファイルを強制的にステージするオプション。1〜3 行目で無視設定を整えた直後に 4 行目で強制追加しているため、巨大な node_modules が追跡されてしまう。>> は追記なので 1 行目は正しく、.gitignore 自体はコミットして共有するのが普通。.gitignore はローカルで即座に効くため push は関係ない。追跡してしまったら git rm -r --cached node_modules で外す。
参考: git-scm.com
この問題を含む Git のテストを制限時間つきで解く
Gitクイズへ