LoginSignup
41
38

More than 3 years have passed since last update.

.gitignoreについて

Last updated at Posted at 2019-07-18

.gitignoreはGitの管理対象から外すためのルールを記述するファイルのこと

注意点としては既にGitに管理(add/commit)されてしまったものについてはignoreされない。対処法については下のほう。

.gitignoreの記述方法

例えばこんなディレクトリ階層を考える。

/Users/sf213471118/git/test
├ .git
├ .gitignore
├ README.md
├ div.md
├ plan
  └ work
├ hoge.js
├ temp.py
└ work
  ├ hoge.py
  ├ huga.js
  ├ common.jar
  └ work
    └ read.rtf

はじめに.gitignore.gitと同階層に置く。

この.gitignoreのファイルに管理対象から外すルールを書き込んでいく。


ディレクトリを無視して除外

例えば階層の異なるhoge.jshoge.pyの2ファイルを除外するのであれば

.gitignore
hoge.js
hoge.py

のように単純にファイル名を列挙すればOK


ワイルドカードを用いて除外

.gitignoreではワイルドカードが使える。

例えば全てのpythonファイルとhugaを含むファイルを除外するのであれば

.gitignore
*.py
huga*

のように記述すればOK


ディレクトリを指定して除外

.gitignoreに記述する条件によっては、条件を満たすignoreしたくないファイルが別階層に存在していることも考えられる。

その場合は.gitignoreをカレントディレクトリとする相対パスを指定する必要がある。

.gitignoreと同階層のファイル/ディレクトリを除外したい場合は先頭に/をつければOK

  • work内のhugaを含むファイル(huga.js)
  • .gitignoreと同階層のhogeを含むファイル(hoge.js)

上記の2ファイルを除外するのであれば

.gitignore
/work/huga*
/hoge*

のように/をつけて記述する。

また例えばread.rtfが格納されているworkフォルダのみ無視する場合は

/work/work/

のように同じく相対パスで記述する必要がある。

よって、以下のように考えることができる

#1
work

#2
/work

#3
work/

#4
/work/

#1 --> 全てのworkフォルダが除外される

#2 --> .gitignoreと同階層のworkフォルダが除外される

#3 --> #1と同義

#4 --> #2と同義


ignoreリストから除外する

条件を重ねていくとignoreされたくないファイルまで含まれてしまうことがある。

その場合は先頭に!をつけてignoreリストから除外する。

しかしディレクトリを無視した場合はその階層以下の特定のファイル/ディレクトリを無視することはできないという制約がある。

git-scm.com
An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn’t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined.

つまり以下のように記述すると、2行目は無視される。

.gitignore
/work/
!/work/common.jar

以上のことから、.gitignoreと同階層のworkフォルダをignoreするが、common.jarのみ残しておきたい時には以下のように記述する

.gitignore
/work/*
!/work/common.jar

これであれば、1行目でignoreしたのはディレクトリではなくファイル/ディレクトリである。

こんな感じでcommon.jarだけであれば大丈夫だが、/work/work/read.rtfはどうやったら救えるのか。。(汗)


既にadd/commitしたものをignoreする場合

Gitに管理されてしまったものはignoreされないので、その場合はcacheを削除する必要がある。

git rm --cached <file or directory>

ファイルを対象とした動作例としてはこんな感じ

terminal
/Users/sf213471118/git/test
$ git rm --cached div.md
rm 'div.md'

ディレクトリを対象とする場合は-rオプションが必要。動作例としてはこんな感じ

terminal
/Users/sf213471118/git/test
$ git rm -r --cached /work
rm 'work/common.jar'
rm 'work/hoge.py'
rm 'work/huga.js'

gitignoreの動作例

.gitignoreはこんな感じで、ディレクトリ階層はこのスレッドを全く同じものを想定。

今回のブランチはmasterで進める。

.gitignore
*.md
/plan
*.py
/work/work
!/div*
!/work/hoge*

pushまでの流れは以下

terminal
$ git add -A
$ git commit -m "commit .ignore"
[master 0ad845f] commit .ignore
 Committer: sf213471118 <xxxx@xxxx>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

$ git push
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 4 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (6/6), 590 bytes | 590.00 KiB/s, done.
Total 6 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 2 local objects.
remote: detect-secrets-stream (beta) ver=151-282d7c1a82922b3275f49f163c77524436ecjfkew FAQ:xxxx
remote: 
remote: Successfully send push metadata.
remote: Push info collect pre-receive hook finished within 3 seconds.
To xxxx:sf213471118/test.git
   52de35b..0sd836a  master -> master

流れを見てみると、push後に

Total 6 (delta 3), reused 0 (delta 0)

とあるので、実際にリモートに同期されたのは6ファイルあると確認できた。

元々.gitignoreの条件を満たすファイルは、

div.md,hoge.js,hoge.py,huga.js,common.jar

の5ファイルで、これがファーストコミットなので.gitignoreを加えて6ファイルであると推測できる。

実際にGitHub上で確認が取れたのでこれで無事完了。

41
38
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
41
38