Tutorial Git Add
Shows the git add command in detail.
The example is a written example from Version Control with Git [1].
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>
The first git status shows you that two files are untracked and reminds you that to make a file tracked, you simply need to use git add.
After git add, both data and .gitignore are staged and tracked, and ready to be added to the repository on the next commit.
In terms of Git’s object model, the entirety of each file at the moment you issued git add was copied into the object store and indexed by its resulting SHA1 name.
Staging a file is also called caching a file or putting a file in the index.
You can use git ls-files to peer under the object model hood and find the SHA1 values for those staged files:
<syntaxhighlight lang="bash">
$ git ls-files --stage
100644 0487f44090ad950f61955271cf0a2d6c6a83ad9a 0 .gitignore
100644 9262d6eedbdd2158bab5c6cebcfd69eeaafb3e69 0 data
</syntaxhighlight>
Now edit the data to contain <syntaxhighlight lang="bash">
- Edits the data
$ nano data ....
- Show the new content of the file data
$ cat data New Data And some more data now
$ git hash-object data 27369b4434874eaa36163e3443c1b0773940224f
- Let’s update the index to contain the new version of the file
$ git add data $ git ls-files --stage 100644 0487f44090ad950f61955271cf0a2d6c6a83ad9a 0 .gitignore 100644 9262d6eedbdd2158bab5c6cebcfd69eeaafb3e69 0 data </syntaxhighlight>
The index now has the updated version of the file.
Again, the file data has been staged, or speaking loosely, the file data is in the index.
The latter phrase is less accurate because the file is actually in the object store and the index merely refers to it.
The seemingly idle play with SHA1 hashes and the index brings home a key point: Think of git add not as add this file, but more as add this content.
See also
- Git, General on Git
- GitHub, General on GitHub
- Tutorial Git Branch, Tutorial on 'git branch' and 'git merge'.
- Tutorial Git Commit, Tutorial on git commit.
- Tutorial Git Diff, Tutorial on 'git diff'
Reference
- ↑ Version Control with Git, Powerful Tools and Techniques for Collaborative Software Development, Jon Loeliger & Matthew McCullough, O'Reilly, 2012 Second Edition, isbn=9781449316389