Tutorial Git Add
Introduction
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.
- untracked, An untracked file is any file not found in either of the previous two categories.
Init the repo
<syntaxhighlight lang="bash"> $ git init
- Initialized empty Git repository in (xx..x)
$ 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)
</syntaxhighlight>
Initially, there are no files and the tracked, ignored, and therefore 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.
Ignoring files
To have Git ignore a file within a directory, simply add that file’s name to the special file .gitignore: <syntaxhighlight lang="bash">
- 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
- nothing added to commit but untracked files present (use "git add" to track)
$ 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
- nothing added to commit but untracked files present (use "git add" to track)
</syntaxhighlight>
Git Add
The command git add stages a file.
In terms of Git’s file classifications, if a file is untracked, then git add converts that file’s status to tracked.
When git add isused on a directory name, all of the files and subdirectories beneath it are staged recursively.
<syntaxhighlight lang="bash">
$ git status
- On branch master
- Initial commit
- Untracked files:
- (use "git add <file>..." to include in what will be committed)
- .gitignore
- data
- Track both new files.
$ git add data .gitignore
$ git status
- On branch master
- Initial commit
- Changes to be committed:
- (use "git rm --cached <file>..." to unstage)
- new file: .gitignore
- new file: data
</syntaxhighlight>