Skip to contentSkip to conversation

user-manual: Add section on ignoring files

PATCH In review Johan Herland 4 participants 9 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 user-manual: Add section on ignoring files

The todo list at the end of the user manual says that something must be said about .gitignore. Also, there seems to be a lack of documentation on how to choose between the various types of ignore files (.gitignore vs. .git/info/exclude, etc.).

This patch adds a section on ignoring files which try to introduce how to tell git about ignored files, and how the different strategies complement eachother.

The syntax of exclude patterns is explained in a simplified manner, with a reference to git-ls-files(1) which already contains a more thorough explanation.

---
Writing this section was triggered by Randal L. Schwartz on #git lamenting the lack of documentation on the difference between .gitignore and .git/info/exclude. Hope this is what you're looking for, Randal. :)

 Documentation/user-manual.txt |   69 +++++++++++++++++++++++++++++++++++++++-
 1 files changed, 67 insertions(+), 2 deletions(-)
Documentation/user-manual.txt +67 -2
index a7abeaa..4ca30dd 100644
@@ -1188,6 +1188,73 @@ description.  Tools that turn commits into email, for example, use
 the first line on the Subject line and the rest of the commit in the
 body.
 
+[[ignoring-files]]
+Ignoring files
+--------------
+
+A project will often generate files that you do 'not' want to track with git.
+This typically includes files generated by a build process or temporary
+backup files made by your editor. Of course, 'not' tracking files with git
+is just a matter of 'not' calling "git add" on them. But it might be
+annoying to have these untracked files automatically showing up in the
+output of "git status", in the commit message template, etc.
+
+Git therefore provides "exclude patterns" for telling git which files to
+actively ignore. Exclude patterns are thoroughly explained in the
+"Exclude Patterns" section of the gitlink:git-ls-files[1] manual page,
+but the heart of the concept is simply a list of files which git should
+ignore. Entries in the list may contain globs to specify multiple files,
+or may be prefixed by "`!`" to explicitly include (un-ignore) a file.
+The following example should illustrate such patterns:
+
+-------------------------------------------------
+# Lines starting with '#' are considered comments.
+# Ignore foo.txt.
+foo.txt
+# Ignore (generated) html files,
+*.html
+# except foo.html which is maintained by hand.
+!foo.html
+# Ignore objects and archives.
+*.[oa]
+-------------------------------------------------
+
+The next question is where to put these exclude patterns so that git can
+find them. Git looks for exclude patterns in the following files:
+
+`.gitignore` files in your working tree:::
+	   You may store multiple `.gitignore` files at various locations in your
+	   working tree. Each `.gitignore` file is applied to the directory where
+	   it's located, including its subdirectories. Furthermore, the
+	   `.gitignore` files can be tracked like any other files in your working
+	   tree; just do a `git add .gitignore` and commit. `.gitignore` is
+	   therefore the perfect place to put exclude patterns that match
+	   ignored files that pop up in every copy of your project, such as
+	   build output files (e.g. `\*.o`), etc.
+`.git/info/exclude` in your repo:::
+	   Exclude patterns in this file are applied to the working tree as a
+	   whole. Since the file is not located in your working tree, it does
+	   not follow push/pull/clone like `.gitignore` can do. This is therefore
+	   the best place to put exclude patterns that you want to keep local
+	   to your copy of the repo, such as temporary backups made by your
+	   editor (e.g. `\*~`), etc.
+The file specified by the `core.excludesfile` config directive:::
+	   By setting the `core.excludesfile` config directive you can tell git
+	   where to find more exclude patterns (see gitlink:git-config[1] for
+	   more information on configuration options). This config directive
+	   can be set in the per-repo `.git/config` file, in which case the
+	   exclude patterns will apply to that repo only. Alternatively, you
+	   can set the directive in the global `~/.gitconfig` file to apply
+	   the exclude pattern to all your git repos. As with the above
+	   `.git/info/exclude` (and, indeed, with git config directives in
+	   general), this directive does not follow push/pull/clone, but stays
+	   local to your repo(s).
+
+[NOTE]
+In addition to the above alternatives, there are git commands that can take
+exclude patterns directly on the command line. See gitlink:git-ls-files[1]
+for an example of this.
+
 [[how-to-merge]]
 How to merge
 ------------
@@ -3184,8 +3251,6 @@ Think about how to create a clear chapter dependency graph that will
 allow people to get to important topics without necessarily reading
 everything in between.
 
-Say something about .gitignore.
-
 Scan Documentation/ for other stuff left out; in particular:
 	howto's
 	some of technical/?
Signature · 1 line
1.5.1.4
#1 lore raw
Quoted 3 lines · Johan Herland

+[[ignoring-files]]
+Ignoring files
+--------------

Looks good ;-).

Documentation/user-manual.txt description. Tools that turn commits into email, for example, use · line 1200

Quoted 6 lines
Documentation/user-manual.txt +6 -0
+A project will often generate files that you do 'not' want to track with git.
+This typically includes files generated by a build process or temporary
+backup files made by your editor. Of course, 'not' tracking files with git
+is just a matter of 'not' calling "git add" on them. But it might be
+annoying to have these untracked files automatically showing up in the
+output of "git status", in the commit message template, etc.

Another point about this annoyance factor is that "git-add ." does not add files that are excluded.

Documentation/user-manual.txt description. Tools that turn commits into email, for example, use · line 1207

Quoted 6 lines
Documentation/user-manual.txt +6 -0
+Git therefore provides "exclude patterns" for telling git which files to
+actively ignore. Exclude patterns are thoroughly explained in the
+"Exclude Patterns" section of the gitlink:git-ls-files[1] manual page,
+but the heart of the concept is simply a list of files which git should
+ignore. Entries in the list may contain globs to specify multiple files,
+or may be prefixed by "`!`" to explicitly include (un-ignore) a file.

I think you can safely teach the reader that later entries override the earlier ones here, as you are about to give such an example just below.

Quoted 16 lines
Documentation/user-manual.txt +15 -0
+The following example should illustrate such patterns:
+
+-------------------------------------------------
+# Lines starting with '#' are considered comments.
+# Ignore foo.txt.
+foo.txt
+# Ignore (generated) html files,
+*.html
+# except foo.html which is maintained by hand.
+!foo.html
+# Ignore objects and archives.
+*.[oa]
+-------------------------------------------------
 
+The next question is where to put these exclude patterns so that git can
+find them. Git looks for exclude patterns in the following files:

Documentation/user-manual.txt description. Tools that turn commits into email, for example, use · line 1233

Quoted 10 lines
Documentation/user-manual.txt +10 -0
+
+`.gitignore` files in your working tree:::
+	   You may store multiple `.gitignore` files at various locations in your
+	   working tree. Each `.gitignore` file is applied to the directory where
+	   it's located, including its subdirectories. Furthermore, the
+	   `.gitignore` files can be tracked like any other files in your working
+	   tree; just do a `git add .gitignore` and commit. `.gitignore` is
+	   therefore the perfect place to put exclude patterns that match
+	   ignored files that pop up in every copy of your project, such as
+	   build output files (e.g. `\*.o`), etc.

... and more importantly, the patterns that are _meant_ to be shared by all the project participants. I think it is probably easier to follow if you explain that in-tree .gitignore is not about personal preference upfront in this section, rather than saying it in .git/info/exclude section.

#2 lore raw

PATCH 1/1 user-manual: Add section on ignoring files

The todo list at the end of the user manual says that something must be said about .gitignore. Also, there seems to be a lack of documentation on how to choose between the various types of ignore files (.gitignore vs. .git/info/exclude, etc.).

This patch adds a section on ignoring files which try to introduce how to tell git about ignored files, and how the different strategies complement eachother.

The syntax of exclude patterns is explained in a simplified manner, with a reference to git-ls-files(1) which already contains a more thorough explanation.

---
Revised version with fixes from Junio and Jakub. Thanks. :)

 Documentation/user-manual.txt |   71 +++++++++++++++++++++++++++++++++++++++-
 1 files changed, 69 insertions(+), 2 deletions(-)
Documentation/user-manual.txt +69 -2
index a7abeaa..750e55a 100644
@@ -1188,6 +1188,75 @@ description.  Tools that turn commits into email, for example, use
 the first line on the Subject line and the rest of the commit in the
 body.
 
+[[ignoring-files]]
+Ignoring files
+--------------
+
+A project will often generate files that you do 'not' want to track with git.
+This typically includes files generated by a build process or temporary
+backup files made by your editor. Of course, 'not' tracking files with git
+is just a matter of 'not' calling "`git add`" on them. But it quickly becomes
+annoying to have these untracked files lying around; e.g. they make
+"`git add .`" and "`git commit -a`" practically useless, and they keep
+showing up in the output of "`git status`", etc.
+
+Git therefore provides "exclude patterns" for telling git which files to
+actively ignore. Exclude patterns are thoroughly explained in the
+"Exclude Patterns" section of the gitlink:git-ls-files[1] manual page,
+but the heart of the concept is simply a list of files which git should
+ignore. Entries in the list may contain globs to specify multiple files,
+or may be prefixed by "`!`" to explicitly include (un-ignore) a previously
+excluded (ignored) file (i.e. later exclude patterns override earlier ones).
+The following example should illustrate such patterns:
+
+-------------------------------------------------
+# Lines starting with '#' are considered comments.
+# Ignore foo.txt.
+foo.txt
+# Ignore (generated) html files,
+*.html
+# except foo.html which is maintained by hand.
+!foo.html
+# Ignore objects and archives.
+*.[oa]
+-------------------------------------------------
+
+The next question is where to put these exclude patterns so that git can
+find them. Git looks for exclude patterns in the following files:
+
+`.gitignore` files in your working tree:::
+	   You may store multiple `.gitignore` files at various locations in your
+	   working tree. Each `.gitignore` file is applied to the directory where
+	   it's located, including its subdirectories. Furthermore, the
+	   `.gitignore` files can be tracked like any other files in your working
+	   tree; just do a "`git add .gitignore`" and commit. `.gitignore` is
+	   therefore the right place to put exclude patterns that are meant to
+	   be shared between all project participants, such as build output files
+	   (e.g. `\*.o`), etc.
+`.git/info/exclude` in your repo:::
+	   Exclude patterns in this file are applied to the working tree as a
+	   whole. Since the file is not located in your working tree, it does
+	   not follow push/pull/clone like `.gitignore` can do. This is therefore
+	   the place to put exclude patterns that are local to your copy of the
+	   repo (i.e. 'not' shared between project participants), such as
+	   temporary backup files made by your editor (e.g. `\*~`), etc.
+The file specified by the `core.excludesfile` config directive:::
+	   By setting the `core.excludesfile` config directive you can tell git
+	   where to find more exclude patterns (see gitlink:git-config[1] for
+	   more information on configuration options). This config directive
+	   can be set in the per-repo `.git/config` file, in which case the
+	   exclude patterns will apply to that repo only. Alternatively, you
+	   can set the directive in the global `~/.gitconfig` file to apply
+	   the exclude pattern to all your git repos. As with the above
+	   `.git/info/exclude` (and, indeed, with git config directives in
+	   general), this directive does not follow push/pull/clone, but remain
+	   local to your repo(s).
+
+[NOTE]
+In addition to the above alternatives, there are git commands that can take
+exclude patterns directly on the command line. See gitlink:git-ls-files[1]
+for an example of this.
+
 [[how-to-merge]]
 How to merge
 ------------
@@ -3184,8 +3253,6 @@ Think about how to create a clear chapter dependency graph that will
 allow people to get to important topics without necessarily reading
 everything in between.
 
-Say something about .gitignore.
-
 Scan Documentation/ for other stuff left out; in particular:
 	howto's
 	some of technical/?
Signature · 1 line
1.5.1.4
#3 lore raw
Quoted 4 lines · Johan Herland

The todo list at the end of the user manual says that something must be said about .gitignore. Also, there seems to be a lack of documentation on how to choose between the various types of ignore files (.gitignore vs. .git/info/exclude, etc.).

Thanks for doing this!

Documentation/user-manual.txt description. Tools that turn commits into email, for example, use · line 1193

Quoted 9 lines
Documentation/user-manual.txt +3 -0
@@ -1188,6 +1188,75 @@ description.  Tools that turn commits into email, for example, use
 the first line on the Subject line and the rest of the commit in the
 body.
 
+[[ignoring-files]]
+Ignoring files
+--------------

This looks like a sensible place to add it....

Documentation/user-manual.txt description. Tools that turn commits into email, for example, use · line 1201

Quoted 7 lines
Documentation/user-manual.txt +7 -0
+A project will often generate files that you do 'not' want to track with git.
+This typically includes files generated by a build process or temporary
+backup files made by your editor. Of course, 'not' tracking files with git
+is just a matter of 'not' calling "`git add`" on them. But it quickly becomes
+annoying to have these untracked files lying around; e.g. they make
+"`git add .`" and "`git commit -a`" practically useless, and they keep
+showing up in the output of "`git status`", etc.

It might be cute to introduce this with an example; "suppose you just imported a new project into git, build it, and run git-status. The output you get might look like:..." etc.

Documentation/user-manual.txt description. Tools that turn commits into email, for example, use · line 1209

Quoted 7 lines
Documentation/user-manual.txt +7 -0
+Git therefore provides "exclude patterns" for telling git which files to
+actively ignore. Exclude patterns are thoroughly explained in the
+"Exclude Patterns" section of the gitlink:git-ls-files[1] manual page,
+but the heart of the concept is simply a list of files which git should
+ignore. Entries in the list may contain globs to specify multiple files,
+or may be prefixed by "`!`" to explicitly include (un-ignore) a previously
+excluded (ignored) file (i.e. later exclude patterns override earlier ones).

It might simplify things just to start off by talking about .gitignore exclusively, rather than talking about exclude patterns in general; something like: "Git therefore allows you to provide .gitignore files, which consist of a list of entries, processed in order from top to bottom, each telling git about some files to ignore...."

Then introduce the other options at the end: "for exclude patterns that you want to be used just by one repo, or by a group of repos on one computer, you can use .git/info/exclude or core.excludesfile." In fact, I'd be *almost* tempted just to leave the discussion of these local exclude files at that, and refer elsewhere for the details. Do people really find them useful?

But those are nits. I'll happily take it as is and then revise later....

--b.

#4 lore raw
Quoted 20 lines · J. Bruce Fields
Quoted 4 lines · Johan Herland

The todo list at the end of the user manual says that something must be said about .gitignore. Also, there seems to be a lack of documentation on how to choose between the various types of ignore files (.gitignore vs. .git/info/exclude, etc.).

Thanks for doing this!

Documentation/user-manual.txt description. Tools that turn commits into email, for example, use · line 1193

Quoted 9 lines
Documentation/user-manual.txt +3 -0
@@ -1188,6 +1188,75 @@ description.  Tools that turn commits into email, for example, use
 the first line on the Subject line and the rest of the commit in the
 body.
 
+[[ignoring-files]]
+Ignoring files
+--------------

This looks like a sensible place to add it....

Quoted 11 lines

Documentation/user-manual.txt description. Tools that turn commits into email, for example, use · line 1201

Quoted 7 lines
Documentation/user-manual.txt +7 -0
+A project will often generate files that you do 'not' want to track with git.
+This typically includes files generated by a build process or temporary
+backup files made by your editor. Of course, 'not' tracking files with git
+is just a matter of 'not' calling "`git add`" on them. But it quickly becomes
+annoying to have these untracked files lying around; e.g. they make
+"`git add .`" and "`git commit -a`" practically useless, and they keep
+showing up in the output of "`git status`", etc.

It might be cute to introduce this with an example; "suppose you just imported a new project into git, build it, and run git-status. The output you get might look like:..." etc.

This would of course work as well, but I was trying to keep it as short as possible. Ignoring files isn't a big part of what git is really about, so I'd like for this section to stay as short and to-the-point as possible.

Quoted 14 lines

Documentation/user-manual.txt description. Tools that turn commits into email, for example, use · line 1209

Quoted 7 lines
Documentation/user-manual.txt +7 -0
+Git therefore provides "exclude patterns" for telling git which files to
+actively ignore. Exclude patterns are thoroughly explained in the
+"Exclude Patterns" section of the gitlink:git-ls-files[1] manual page,
+but the heart of the concept is simply a list of files which git should
+ignore. Entries in the list may contain globs to specify multiple files,
+or may be prefixed by "`!`" to explicitly include (un-ignore) a previously
+excluded (ignored) file (i.e. later exclude patterns override earlier ones).

It might simplify things just to start off by talking about .gitignore exclusively, rather than talking about exclude patterns in general; something like: "Git therefore allows you to provide .gitignore files, which consist of a list of entries, processed in order from top to bottom, each telling git about some files to ignore...."

Quoted 6 lines

Then introduce the other options at the end: "for exclude patterns that you want to be used just by one repo, or by a group of repos on one computer, you can use .git/info/exclude or core.excludesfile." In fact, I'd be *almost* tempted just to leave the discussion of these local exclude files at that, and refer elsewhere for the details. Do people really find them useful?

Sounds like a good idea. Feel free to rewrite. But the original impetus for the section was Randal's question on the difference between the various ignore files, so make sure that's still in there somewhere.

Quoted 2 lines

But those are nits. I'll happily take it as is and then revise later....

Thanks :)

Have fun!

...Johan

Signature · 2 lines
Johan Herland, <[email protected]>
www.herland.net
Acked-by on v1 1/1
#5 lore raw
In reply to Johan Herland

Documentation/user-manual.txt description. Tools that turn commits into email, for example, use · line 1200

Quoted 5 lines · Johan Herland
Documentation/user-manual.txt +5 -0
+This typically includes files generated by a build process or temporary
+backup files made by your editor. Of course, 'not' tracking files with git
+is just a matter of 'not' calling "`git add`" on them. But it quickly becomes
+annoying to have these untracked files lying around; e.g. they make
+"`git add .`" and "`git commit -a`" practically useless, and they keep

I think we would want to s/and "git commit -a//; if you start tracking the file, .gitignore would not interfere with it AFAIK.

Other than that,

#6 lore raw
Quoted 10 lines · Junio C Hamano

Documentation/user-manual.txt description. Tools that turn commits into email, for example, use · line 1200

Quoted 5 lines · Johan Herland
Documentation/user-manual.txt +5 -0
+This typically includes files generated by a build process or temporary
+backup files made by your editor. Of course, 'not' tracking files with git
+is just a matter of 'not' calling "`git add`" on them. But it quickly becomes
+annoying to have these untracked files lying around; e.g. they make
+"`git add .`" and "`git commit -a`" practically useless, and they keep

I think we would want to s/and "git commit -a//; if you start tracking the file, .gitignore would not interfere with it AFAIK.

You're right. "git commit -a" doesn't actually add untracked files at all (you still have to do "git add", anyway). Don't know what I was thinking. Feel free to edit.

...Johan

Signature · 2 lines
Johan Herland, <[email protected]>
www.herland.net
#7 lore raw
In reply to Johan Herland

Documentation/user-manual.txt description. Tools that turn commits into email, for example, use · line 1200

Quoted 6 lines · Johan Herland
Documentation/user-manual.txt +6 -0
+A project will often generate files that you do 'not' want to track with git.
+This typically includes files generated by a build process or temporary
+backup files made by your editor. Of course, 'not' tracking files with git
+is just a matter of 'not' calling "git add" on them. But it might be
+annoying to have these untracked files automatically showing up in the
+output of "git status", in the commit message template, etc.

_And_ ignored files affect globbing and path patterns ("git add ." or "git add *.txt"would not add ignored files), and --all options ("git commit -a" would not commit changed but ignored files).

Signature · 3 lines
Jakub Narebski
Warsaw, Poland
ShadeHawk on #git
#8 lore raw
Quoted 12 lines · Jakub Narebski

Documentation/user-manual.txt description. Tools that turn commits into email, for example, use · line 1200

Quoted 6 lines · Johan Herland
Documentation/user-manual.txt +6 -0
+A project will often generate files that you do 'not' want to track with git.
+This typically includes files generated by a build process or temporary
+backup files made by your editor. Of course, 'not' tracking files with git
+is just a matter of 'not' calling "git add" on them. But it might be
+annoying to have these untracked files automatically showing up in the
+output of "git status", in the commit message template, etc.

_And_ ignored files affect globbing and path patterns ("git add ." or "git add *.txt"would not add ignored files), and --all options ("git commit -a" would not commit changed but ignored files).

Yes, of course. I forgot about that "minor" point...

Have fun!

...Johan

Signature · 2 lines
Johan Herland, <[email protected]>
www.herland.net