-
Three ways of excluding files.
Almost every git user knows about adding a
.gitignorefile to root of the project to control the visibility of files and folders. Because this file is in the repository; this configuration also applies to remote repositories of the project. But it’s not the only way. I’m going to tell you how to get Git to ignore files on a per computer and per repository basis. These could be better choices in some circumstances.There are three types of exclude files; from highest to lowest order of precedence they are:
Per Project: .gitignore file in the repository
This is the usual way of adding an ignore file. Call it
.gitignoreand save it to the root of your project to apply to all the files (you can add different.gitignorefiles in subdirectories where they have lesser scope). It is a part of the repository, so it will need to begit-added and committed for each change. This is useful for repositories that are passed around with others who may not have a per computer exclude file, or when there are project specific files that need to be taken into account.Even easier if you have per computer file, you can copy it straight in to your project with a simple
cp ~/.gitignore .gitignoreand edit to handle your specific requirements.Per Repository: in .git/info/excludes
You can exclude files on a per repository basis by editing the
.git/info/excludesfile in your repository. (Why it takes it from this location rather than.git/configI don’t know: add it to the list of git annoyances). These exclusions (or inclusions, you can override the higher level exclusions by prepending!to lines that you want to include) are not shared with the working directory, so they only apply to that particular repository, and are not shared with any remotes. This is useful when you have particular requirements because of your workflow or machine setup.Per Computer: through settings in ~/.gitconfig
There should already be a
.gitconfigfile in your home directory. This is where the global setting for your git installation are stored; such as the user’s name and email address. Within this you can set a path to an excludes file that will apply to all git repositories on the computer in the same way as the name and email defined in this file apply to all repositories.For example: Most of what I do is in Xcode so I have the following ~/.gitignore file
# Mac OS X *.DS_Store # Xcode *.pbxuser *.mode1v3 *.mode2v3 *.perspectivev3 *.xcuserstate project.xcworkspace/ xcuserdata/ # Generated files build/ *.[oa] *.pyc # Backup files *~.nibAnd in my
.gitconfigfile under the[core]section I’ve added the path to this file for theexcludesfilekey.[core]
excludesfile = ~/.gitignoreNow I have a standard set of ignores that apply to all my git repositories on this machine without me having to add a specific
.gitignorefile to each one. This is probably most useful if you create a lot of repositories for yourself, but I recommend it to everyone. It’s lowest on the precedence scale and provides a neat catch-all.Summary
Most of the time the first solution is quite adequate, having exclusions within a repository that is likely to have a public face is probably the most effective way of managing file visibility. But, as with most of Git, there are ways of handling edge cases. You just need to know that they are there.
REFERENCE
The gitignore man page.
Note
This may seem familiar, I’ve taken it from a post on my personal site.
Posted on April 13, 2010 with 36 notes ()
-
viewpointhg9d likes this
-
allergplus likes this
-
mycocoaheads reblogged this from 365git and added:
If you’re both
-
365git posted this
-