Gitチュートリアル


Gitと{{title}}


Gitの貢献


Git Advanced


Git Undo




GitIgnoreと.gitignore


Git無視

コードを他の人と共有する場合、プロジェクトのファイルや部分があり、共有したくないことがよくあります。

  • ログファイル
  • 一時ファイル
  • 隠しファイル
  • 個人ファイル

Gitは、ファイルを使用してGitがプロジェクトのどのファイルまたは部分を無視するかを指定でき.gitignoreます。

Gitはで指定されたファイルとフォルダーを追跡しません.gitignoreただし、.gitignore ファイル自体はGitによって追跡されます。


.gitignoreを作成します

ファイルを作成する.gitignoreには、ローカルGitのルートに移動して、次のように作成します。

touch .gitignore

次に、テキストエディタを使用してファイルを開きます。

2つの簡単なルールを追加します。

  • .log拡張子が付いているファイルを無視します
  • 名前の付いたディレクトリ内のすべてを無視しますtemp

# ignore ALL .log files
*.log

# ignore ALL files in ANY directory named temp
temp/

.logこれで、すべてのファイルと フォルダー内のすべてtempがGitによって無視されます。

注:この場合、.gitignoreリポジトリ全体に適用される単一のものを使用します。

.gitignoreサブディレクトリに追加のファイルを含めることもできます。これらは、そのディレクトリ内のファイルまたはフォルダにのみ適用されます。



.gitignoreのルール

.gitignore ファイル内 のパターンを照合するための一般的なルールは次のとおりです。

Pattern Explanation/Matches Examples
  Blank lines are ignored  
# text comment Lines starting with # are ignored  
name All name files, name folders, and files and folders in any name folder /name.log
/name/file.txt
/lib/name.log
name/ Ending with / specifies the pattern is for a folder. Matches all files and folders in any name folder /name/file.txt
/name/log/name.log

no match:
/name.log
name.file All files with the name.file /name.file
/lib/name.file
/name.file Starting with / specifies the pattern matches only files in the root folder /name.file

no match:
/lib/name.file
lib/name.file Patterns specifiing files in specific folders are always realative to root (even if you do not start with / ) /lib/name.file

no match:
name.file
/test/lib/name.file
**/lib/name.file Starting with ** before / specifies that it matches any folder in the repository. Not just on root. /lib/name.file
/test/lib/name.file
**/name All name folders, and files and folders in any name folder /name/log.file
/lib/name/log.file
/name/lib/log.file
/lib/**/name All name folders, and files and folders in any name folder within the lib folder. /lib/name/log.file
/lib/test/name/log.file
/lib/test/ver1/name/log.file

no match:
/name/log.file
*.file All files withe .file extention /name.file
/lib/name.file
*name/ All folders ending with name /lastname/log.file
/firstname/log.file
name?.file ? matches a single non-specific character /names.file
/name1.file

no match:
/names1.file
name[a-z].file [range] matches a single character in the specified range (in this case a character in the range of a-z, and also be numberic.) /names.file
/nameb.file

no match:
/name1.file
name[abc].file [set] matches a single character in the specified set of characters (in this case either a, b, or c) /namea.file
/nameb.file

no match:
/names.file
name[!abc].file [!set] matches a single character, except the ones spesified in the set of characters (in this case a, b, or c) /names.file
/namex.file

no match:
/namesb.file
*.file All files withe .file extention /name.file
/lib/name.file
name/
!name/secret.log
! specifies a negation or exception. Matches all files and folders in any name folder, except name/secret.log /name/file.txt
/name/log/name.log

no match:
/name/secret.log
*.file
!name.file
! specifies a negation or exception. All files withe .file extention, except name.file /log.file
/lastname.file

no match:
/name.file
*.file
!name/*.file
junk.*
Adding new patterns after a negation will re-ignore a previous negated file
All files withe .file extention, except the ones in name folder. Unless the file name is junk
/log.file
/name/log.file

no match:
/name/junk.file

ローカルおよび個人のGit無視ルール

ファイルやフォルダを無視して、配布された.gitignoreファイルに表示しないようにすることもできます。

これらの種類の無視は .git/info/excludeファイルで指定されています。と同じように機能します .gitignoreが、他の人には表示されません。


エクササイズで自分をテストする

エクササイズ:

すべてのファイル.gitignoreを無視する行を追加します.temp