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
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With