Reputation:
Does a .gitignore file in Master branch apply to other branches?
In section "Create a local .gitignore" in https://help.github.com/en/articles/ignoring-files, it doesn't specify which branch you should be in.
Upvotes: 8
Views: 3876
Reputation: 139601
Different branches may contain different .gitignore
files, and each one applies to the branches it appears on. To make rules that apply to all branches, edit .git/info/exclude
.
The gitignore documentation covers multiple files that can contain ignore patterns.
Which file to place a pattern in depends on how the pattern is meant to be used.
Patterns which should be version-controlled and distributed to other repositories via clone (i.e., files that all developers will want to ignore) should go into a
.gitignore
file.Patterns which are specific to a particular repository but which do not need to be shared with other related repositories (e.g., auxiliary files that live inside the repository but are specific to one user’s workflow) should go into the
$GIT_DIR/info/exclude
file.Patterns which a user wants Git to ignore in all situations (e.g., backup or temporary files generated by the user’s editor of choice) generally go into a file specified by
core.excludesFile
in the user’s~/.gitconfig
. Its default value is$XDG_CONFIG_HOME/git/ignore
. If$XDG_CONFIG_HOME
is either not set or empty,$HOME/.config/git/ignore
is used instead.
Your question asked about a .gitignore
file on the master branch and thus corresponds to the first bullet.
Ignore patterns apply to the branches they appear on and more specifically to the directory subtree where each is found, perhaps overridden by .gitignore
files in deeper directories. Also from the docs:
- Patterns read from a
.gitignore
file in the same directory as the path, or in any parent directory, with patterns in the higher level files (up to the toplevel of the work tree) being overridden by those in lower level files down to the directory containing the file. These patterns match relative to the location of the.gitignore
file. A project normally includes such.gitignore
files in its repository, containing patterns for files generated as part of the project build.
Upvotes: 3
Reputation: 489073
As Obsidian Age said, it applies to the files in the sub-tree rooted at the point in which the .gitignore
file appears.
A .gitignore
file should be1 tracked (i.e., should be present in the index) and therefore exist as a copy in each commit. This means each commit has its own .gitignore
copy.2 If you're on branch-1
and you git checkout branch-2
and that brings out a different .gitignore
file, its contents now apply to the folder and subfolders. If you then git checkout branch-1
to bring back the branch-1
files, including the branch-1
version of .gitignore
, those contents now apply.
1This isn't a hard requirement, but not committing the .gitignore
file means you probably should not be using a .gitignore
file at all. You can get a similar effect by creating either a global gitignore (for yourself), or a .git/info/excludes
file for this one repository.
2As with all tracked-and-committed files, Git shares any file that exactly matches. It can do this because every committed file is frozen forever in that particular form, in that particular commit. It can't be changed, so it can be shared.
That is, suppose you git checkout master
, create a ten-megabyte file named big
, run git add big
, and then git commit
. You've now created a new 10-MB file that is in the commit you just made. Every commit you make after this point also has a 10-MB file in it, up until you git rm big
to remove it from the index-and-work-tree and make all future commits not have the file.3 But every file that shares that exact version of the big file, uses one single shared copy. So if you have four thousand commits, but they all have just that one particular file big
, you only have 10 MB of big
, not 4000*10 MB.
3Or, of course, you could overwrite big
with tiny contents. The next commit you make will have a small file named big
, which will get shared with every future commit, just as the big file named big
got shared with every future commit up to this point.
If that's too confusing, just think of Git as automatically doing a "see if this data ever got committed before" de-duplication pass, every time you git add
a file. If it did ever get committed before, Git automatically re-uses the already-frozen copy.
Upvotes: 0
Reputation: 42354
.gitignore
applies to files in the folder that the .gitignore
file is in; it does not matter which branch you currently have selected. Any files specified in the .gitignore
will be excluded from Git.
Note that the references in .gitignore
are recursive, so if you have a .gitignore
file at the root level of your project, it will apply the rules to all files and folders within your project.
Upvotes: 5