LoginSignup
81
61

More than 3 years have passed since last update.

.gitignoreでgitにコミットしないディレクトリを管理する【node_modules】

Last updated at Posted at 2019-08-11

node_modulesなどはgitにcommit、pushする必要はない

node.jsの開発において、

npm install --save [library name]

でインストールしたバッケージは、package.jsonのdependenciesにて依存関係が定義されている。

{
  "name": "project-name",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "cheerio": "^1.0.0-rc.3",
    "puppeteer": "^1.19.0",
    "request": "^2.88.0",
    "request-promise": "^4.2.4"
  }
}

npm installを実行すれば、package.jsonの依存関係を参照して必要なライブラリがダウンロードされる。
したがって、ライブラリの実体が保存されているnode_modulesを含める必要がない。
プロジェクトの容量が無駄に大きくなってしまうので、.gitignoreファイルでcommit対象から外して管理するのがいい。

.gitignoreファイルの作成

.gitignoreの作成は、通常のファイル作成と同様に

touch .gitignore

で作られる。
ファイルは、ワーキングディレクトリ直下(プロジェクトの最上位のディレクトリ)に作成する。
.gitディレクトリに置くと読み込まれないので注意。

.gitignoreの記述方法

下記のように記述できる。

# ディレクトリを指定する場合
node_modules/

# ファイルのパターン一致でコンパイルなどで生成したファイルを指定する場合
*.com
*.class
*.dll
*.exe
*.o
*.so

ディレクトリの場合はディレクトリ/で指定でき、
ファイルの場合は正規表現で指定できる。

設定後、実際にcommitしてみて確認

.gitignoreを設定後、commitすると

$ git add *
The following paths are ignored by one of your .gitignore files:
node_modules

のように除外されたパスが表示される。
意図通り除外されているか確認出来る。

参考書籍

GitHub実践入門
弊社での新卒エンジニア向け課題図書になった。
必要なコマンドを随時調べるのも良いけど、最初に一通り理解して置くとスムーズにいく。

81
61
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
81
61