Tutorial Git Add

From HaFrWiki
Jump to: navigation, search

Shows the git add command in detail.
The example is a written example from Version Control with Git [1].


Introduction[edit]

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[edit]

<syntaxhighlight lang="bash"> $ git init

  1. Initialized empty Git repository in (xx..x)

$ git status

  1. On branch master
  2. Initial commit
  3. nothing to commit (create/copy files and use "git add" to track)

$ echo "New data" > data

$ git status

  1. On branch master
  2. Initial commit
  3. Untracked files:
  4. (use "git add <file>..." to include in what will be committed)
  5. data
  6. 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[edit]

To have Git ignore a file within a directory, simply add that file’s name to the special file .gitignore: <syntaxhighlight lang="bash">

  1. Manually create an example junk file

$ touch main.o

$git status

  1. On branch master
  2. Initial commit
  3. Untracked files:
  4. (use "git add <file>..." to include in what will be committed)
  5. data
  6. main.o
  7. nothing added to commit but untracked files present (use "git add" to track)

$ echo main.o > .gitignore

$ git status

  1. On branch master
  2. Initial commit
  3. Untracked files:
  4. (use "git add <file>..." to include in what will be committed)
  5. .gitignore
  6. data
  7. nothing added to commit but untracked files present (use "git add" to track)

</syntaxhighlight>

Git Add[edit]

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

  1. On branch master
  2. Initial commit
  3. Untracked files:
  4. (use "git add <file>..." to include in what will be committed)
  5. .gitignore
  6. data
  1. Track both new files.

$ git add data .gitignore

$ git status

  1. On branch master
  2. Initial commit
  3. Changes to be committed:
  4. (use "git rm --cached <file>..." to unstage)
  5. new file: .gitignore
  6. 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">

  1. Edits the data

$ nano data ....

  1. Show the new content of the file data

$ cat data New Data And some more data now

$ git hash-object data 27369b4434874eaa36163e3443c1b0773940224f

  1. 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[edit]

top

Reference[edit]

top

  1. Version Control with Git, Powerful Tools and Techniques for Collaborative Software Development, Jon Loeliger & Matthew McCullough, O'Reilly, 2012 Second Edition, isbn=9781449316389