8.1. Ignoring files and directories with a .gitignore file
Git can be configured to ignore certain files and directories for repository operations. This is configured via one or several
.gitignore
files. Typically, this file is located at the root of your Git repository but it can also be located in sub-directories. In the second case the defined rules are only valid for the sub-directory and below.
You can use certain wildcards in this file.
*
matches several characters.
For example, the following
.gitignore
file tells Git to ignore the bin
and target
directories and all files ending with a ~.# ignore all bin directories
# matches "bin" in any subfolder
bin/
# ignore all target directories
target/
# ignore all files ending with ~
*~
You can create the
.gitignore
file in the root directory of the working tree to make it specific for the Git repository.
The
.gitignore file tells Git to ignore the specified files in Git commands. You can still add ignored files to the staging area of the Git repository by using the --force parameter, i.e. with the git add --force [paths] command.
This is useful if you want to add, for example, auto-generated binaries, but you need to have a fine control about the version which is added and want to exclude them from the normal workflow.
|
It is good practice to commit the local
.gitignore
file into the Git repository so that everyone who clones this repository have it.8.2. Stop tracking files based on the .gitignore file
Files that are tracked by Git are not automatically removed if you add them to a
.gitignore
file. Git never ignores files which are already tracked, so changes in the .gitignore
file only affect new files. If you want to ignore files which are already tracked you need to explicitly remove them.
The following command demonstrates how to remove the
.metadata
directory and the doNotTrackFile.txt
file from being tracked. This is example code, as you did not commit the corresponding files in your example, the command will not work in your Git repository.# remove directory .metadata from git repo
git rm -r --cached .metadata
# remove file test.txt from repo
git rm --cached doNotTrackFile.txt
Adding a file to the
.gitignore
file does not remove the file from the repository history. If the file should also be removed from the history, have a look at the git filter-branch
command which allows you to rewrite the commit history.8.3. Global (cross-repository) .gitignore settings
You can also setup a global
.gitignore
file valid for all Git repositories via the core.excludesfile
setting. The setup of this setting is demonstrated in the following code snippet.# Create a ~/.gitignore in your user directory
cd ~/
touch .gitignore
# Exclude bin and .metadata directories
echo "bin" >> .gitignore
echo ".metadata" >> .gitignore
echo "*~" >> .gitignore
echo "target/" >> .gitignore
# for Mac
echo ".DS_Store" >> .gitignore
echo "._*" >> .gitignore
# Configure Git to use this file
# as global .gitignore
git config --global core.excludesfile ~/.gitignore
The global
.gitignore
file is only locally available.8.4. Local per-repository ignore rules
You can also create local per-repository rules by editing the
.git/info/exclude
file in your repository. These rules are not committed with the repository so they are not shared with others.
This allows you to exclude, for example, locally generated files.
8.5. Tracking empty directories with Git
Git ignores empty directories, i.e., it does not put them under version control. If you want to track an empty directory in your Git repository, it is a good practice to put a file called
.gitignore
in the directory. As the directory now contains a file, Git includes it into its version control mechanism.
The file could be called anything. Some people suggest to call the file
.gitkeep . One problem with this approach is that .gitkeep is unlikely to be ignored by build systems. This may result in the .gitkeep file being copied to the output repository, which is typically not desired. |
0 comments:
Post a Comment