File Classifications in Git

Git classifies your files into three groups:

Tracked

A tracked file is any file already in the repository or any file that is staged in the index. To add a new file, somefile, to this group, run git add somefile.

Ignored

An ignored file must be explicitly declared invisible or ignored in the repository, even though it may be present within your working directory. A software project tends to have a good number of ignored files. Common ignored files include temporary and scratch files, personal notes, compiler output, and most files generated automatically during a build. Git maintains a default list of files to ignore, and you can configure your repository to recognize others. Ignored files are discussed in detail in The .gitignore File.

Untracked

An untracked file is any file not found in either of the previous two categories. Git considers the entire set of files in your working directory and subtracts both the tracked files and the ignored files to yield what is untracked.

Let’s explore the different categories of files by creating a brand-new working directory and repository and then working with some files:

$ cd /tmp/my_stuff
$ git init

$ git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)

$ echo "New data" > data

$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       data
nothing added to commit but untracked files present (use "git add" to track)

Initially, there are no files, and the tracked, ignored, and untracked sets are empty. Once you create data, git status reports a single, untracked file.

Editors and build environments often leave temporary or transient files among your source code. Such files usually shouldn’t be tracked as source files in a repository. To have Git ignore a file within a directory, simply add that file’s name to the special file, .gitignore:

# Manually create an example junk file
$ touch main.o

$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       data
#       main.o

$ echo main.o > .gitignore
$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       .gitignore
#       data

Thus main.o is ignored, but git status now shows a new, untracked file called .gitignore. Although the .gitignore file has special meaning to Git, it is managed just like any other normal file within your repository. Until .gitignore is added, Git considers it untracked.

The next few sections demonstrate different ways to change the tracked status of a file as well as how to add or remove it from the index.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.104.196