LoginSignup
28
19

More than 5 years have passed since last update.

.gitignoreファイルを使ってみよう

Last updated at Posted at 2017-09-06

.gitignoreファイルを使ってみよう

概要

.gitignoreファイルを使って複数のファイルをGitの管理の対象外にする方法をまとめました。

環境

Git 2.13.1
Windows8

詳細

.gitignoreというファイルはGitの管理下に置きたくないファイルを指定できます。

まずは、git initコマンドを使ったディレクトリで.gitignoreファイルを作成します。
WindowsのGUIから作成する場合、.gitignoreという名前で作成しようとすると「ファイル名を入力してください」とシステムに怒られます。
拡張子と勘違いされたのでしょう。
この問題を回避するためには「.gitignore.」という名前でファイルを作成すると解決します。

キャプチャ3.JPG

.gitignoreファイルが作成されました。

キャプチャ.JPG

.gitignoreファイルが作成されたので早速使ってみましょう。
今回は、テスト用のファイルとしてtestfile1.txtからtestfile10.txtを用意しました。

git statusコマンドを叩いて作成されているファイルを確認します。

command
git status
コマンドプロンプトの表示

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .gitignore
        testfile1.txt
        testfile10.txt
        testfile2.txt
        testfile4.txt
        testfile5.txt
        testfile7.txt
        testfile8.txt
        testfile9.txt

nothing added to commit but untracked files present (use "git add" to track)

しっかりとgitの管理下にtestfile1.txtからtestfile10.txtが入っています。

testfile3.txtからtestfile6.txtのファイルを.gitignoreを使って
Gitの管理から外そうと思います。

.gitignoreに対してGitの管理から外したいファイルを記述します。

.gitignore
#下に記述されたファイルがgitの管理から外れるよー

testfile3.txt
testfile4.txt
testfile5.txt
testfile6.txt

本当に管理から外れたのでしょうか。
再びgit statusコマンドをたたいて確認してみます。

command
git status
コマンドプロンプトの表示

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .gitignore
        testfile1.txt
        testfile10.txt
        testfile2.txt
        testfile7.txt
        testfile8.txt
        testfile9.txt

nothing added to commit but untracked files present (use "git add" to track)

testfile3.txtからtestfile6.txtのファイルが管理から外れていることが確認できます。

しかし、個人的に.gitignoreファイルをもっと短縮して書けるのではないかと気になったので、書き直すことにしました。

結果、以下のような短縮ができました。

.gitignore
#下に記述されたファイルがgitの管理から外れるよー

testfile[3-6].txt

今回この記事を書いたおかげで.gitignoreファイルという存在とその使い方について知ることができました。
今後の開発にガシガシ生かしていきたいと思います。

以上です。

28
19
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
28
19