This is an old revision of the document!


How to stage changes in your working tree in the index to make them part of the next commit.

The two commands that stage your changes in the index:

  • git add: Stage both new files and changes to existing files
  • git rm: Stage the deletion of currently tracked files

IMPORTANT: You must stage your changes in the index for them to be part of the next commit – changes only in your working tree have no effect on the next commit.

ALSO IMPORTANT: If you're experimenting, you can run:

$ git reset --hard

to undo all your local changes and staged content and start over.

Before staging:

$ git status
On branch master
Your branch is up to date with 'rpjday/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	newdir/
	newfile

nothing added to commit but untracked files present (use "git add" to track)
$

Staging:

$ git add newfile newdir

After staging:

$ git status
On branch master
Your branch is up to date with 'rpjday/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   newdir/google-chrome
	new file:   newdir/grub
	new file:   newdir/useradd
	new file:   newfile

$

Before staging:

$ git status
On branch master
Your branch is up to date with 'rpjday/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   README.asc

no changes added to commit (use "git add" and/or "git commit -a")
$

Staging:

$ git add README.asc

After staging:

$ git status
On branch master
Your branch is up to date with 'rpjday/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   README.asc

$

Before removal:

$ git status
On branch master
Your branch is up to date with 'rpjday/master'.

nothing to commit, working tree clean
$

Git removal:

$ git rm README.asc
$ git rm -r theme

After Git removal:

$ git status
On branch master
Your branch is up to date with 'rpjday/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	deleted:    README.asc
	deleted:    theme/epub/epub.css
	deleted:    theme/epub/epub.xsl
	deleted:    theme/epub/layout.html
	deleted:    theme/html/html.css
	deleted:    theme/html/html.xsl
	deleted:    theme/mobi/mobi.css
	deleted:    theme/mobi/mobi.xsl
	deleted:    theme/pdf/pdf.css
	deleted:    theme/pdf/pdf.xsl

$

Stage all changes and deletions of existing files, but not addition of new files:

-u, --update
    Update the index just where it already has an entry
    matching <pathspec>. This removes as well as modifies index
    entries to match the working tree, but adds no new files.

    If no <pathspec> is given when -u option is used, all
    tracked files in the entire working tree are updated (old
    versions of Git used to limit the update to the current
    directory and its subdirectories).

Stage all changes, additions and deletions:

-A, --all, --no-ignore-removal
    Update the index not only where the working tree has a file
    matching <pathspec> but also where the index already has an
    entry. This adds, modifies, and removes index entries to
    match the working tree.

    If no <pathspec> is given when -A option is used, all files
    in the entire working tree are updated (old versions of Git
    used to limit the update to the current directory and its
    subdirectories).

Stage changes and additions, but not deletions:

--no-all, --ignore-removal
    Update the index by adding new files that are unknown to
    the index and files modified in the working tree, but
    ignore files that have been removed from the working tree.
    This option is a no-op when no <pathspec> is used.

    This option is primarily to help users who are used to
    older versions of Git, whose "git add <pathspec>..." was a
    synonym for "git add --no-all <pathspec>...", i.e. ignored
    removed files.
--cached
    Use this option to unstage and remove paths only from the
    index. Working tree files, whether modified or not, will be
    left alone.
-f, --force
    Override the up-to-date check.

where:

The files being removed have to be identical to the tip
of the branch, and no updates to their contents can be 
staged in the index, though that default behavior can be
overridden with the -f option.
  • git_add_rm.1551366029.txt.gz
  • Last modified: 2019/02/28 15:00
  • by rpjday