User Tools

Site Tools


git_add_rm

This is an old revision of the document!


Overview

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

How staging works and what it does

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.

Basic "git add" and "git rm"

Staging new content

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

$

Staging changed content

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

$

Removing existing content

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

$

Advanced "git add" operations

"-u", "--update" option

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).

"-A", "--all" option

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).

"--no-all" option

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.

Advanced "git rm" operations

"--cached" option

--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" option

-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.

Example:

$ 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"
$
$ git rm README.asc 
error: the following file has local modifications:
    README.asc
(use --cached to keep the file, or -f to force removal)
$

"git rm" and empty directories

Since Git cannot version empty directories, any git rm command that results in any empty directories will result in the removal of all resulting empty directories.

Accessing deleted files

Listing all deleted files throughout history

$ git log --diff-filter=D --summary
commit 97b26b7710aed5d41a6a079fbc7279e4bc31e1f5
Author: Adrien Ollier <aollier@users.noreply.github.com>
Date:   Mon Nov 12 12:40:05 2018 +0100

    removed unused images

 delete mode 100644 images/emoji.png
 delete mode 100644 images/notifications.png

commit 99c2df95f5839f6c6df75eedbfc06cac35389d91
Author: Adrien Ollier <aollier@users.noreply.github.com>
Date:   Thu Jun 15 12:52:03 2017 +0200

    removed unused images

 delete mode 100644 book/04-git-server/images/gitlab-broadcast.png
 delete mode 100644 book/05-distributed-git/images/merging-workflows-4 2.png
 delete mode 100644 book/06-github/images/blink-pull-request-open copy.png
 delete mode 100644 book/06-github/images/blink-pull-request-open.png
 delete mode 100644 book/06-github/images/hubot.png
... snip ...

Recovering a deleted file

Display the most recent commit that references the file:

$ git log -1 -- OUTLINE.txt
commit b86814bf758645cf1d0ea8a6cbfd1486d257f4dc
Author: Scott Chacon <schacon@gmail.com>
Date:   Tue Oct 14 10:55:34 2014 +0200

    remove changes notes and initial outline
$

Now display its content from the previous commit (optionally redirect to file):

$ git show b86814bf758645cf1d0ea8a6cbfd1486d257f4dc^:OUTLINE.txt
1. Getting Started
1.1 About Version Control
    * Local Version Control Systems
    * Centralized Version Control Systems
    * Distributed Version Control Systems
... snip ...

You can also:

$ git rev-list -n 1 HEAD -- OUTLINE.txt
b86814bf758645cf1d0ea8a6cbfd1486d257f4dc
$
git_add_rm.1552486577.txt.gz · Last modified: 2019/03/13 14:16 by rpjday