Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Staging Deleted files

Say I have a file in my git repository called foo.

Suppose it has been deleted with rm (not git rm). Then git status will show:

Changes not staged for commit:      deleted: foo 

How do I stage this individual file deletion?

If I try:

git add foo 

It says:

'foo' did not match any files. 

Update (9 years later, lol):

This looks like it has been fixed in git 2.x:

$ git --version git version 2.25.1  $ mkdir repo  $ cd repo  $ git init . Initialized empty Git repository in repo/.git/  $ touch foo bar baz  $ git add foo bar baz  $ git commit -m "initial commit" [master (root-commit) 79c736b] initial commit 3 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 bar create mode 100644 baz create mode 100644 foo  $ rm foo  $ git status On branch master Changes not staged for commit:     deleted: foo  $ git add foo  $ git status On branch master Changes to be committed:     deleted:    foo 
like image 239
Andrew Tomazos Avatar asked Sep 11 '12 16:09

Andrew Tomazos


People also ask

How can I add a deleted file to stage?

In order to add all deleted and modified files only to your staging area, you have to use the “git add” command followed by the “-u” option. As an example, let's say that we modified one of our files, deleted one and added one to our working directory.

What does staging files mean?

These files are also referred to as "untracked files." Staging area is files that are going to be a part of the next commit, which lets git know what changes in the file are going to occur for the next commit. The repository contains all of a project's commits.

How do I Unstage a staged file in git?

To unstage commits on Git, use the “git reset” command with the “–soft” option and specify the commit hash. Alternatively, if you want to unstage your last commit, you can the “HEAD” notation in order to revert it easily. Using the “–soft” argument, changes are kept in your working directory and index.


2 Answers

Use git rm foo to stage the file for deletion. (This will also delete the file from the file system, if it hadn't been previously deleted. It can, of course, be restored from git, since it was previously checked in.)

To stage the file for deletion without deleting it from the file system, use git rm --cached foo

like image 52
Wooble Avatar answered Sep 20 '22 15:09

Wooble


Even though it's correct to use git rm [FILE], alternatively, you could do git add -u.

According to the git-add documentation:

-u --update

Update the index just where it already has an entry matching [FILE]. This removes as well as modifies index entries to match the working tree, but adds no new files.

If no [FILE] 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).

Upon which the index will be refreshed and files will be properly staged.

like image 42
Sailesh Avatar answered Sep 18 '22 15:09

Sailesh