Tutorial Git Add

From HaFrWiki
Revision as of 12:39, 8 July 2017 by Hjmf (talk | contribs) (Created page with "{{TOCright}} == Introduction == Git classifies your files into three groups: * '''tracked''', A tracked file is any file already in the repository or any file that is stage...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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

  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

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

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>

See also

top

Reference

top