How do you remove a file from the staging area in Git?

How do you remove a file from the staging area in Git?

a) git reset
b) git delete
c) git remove
d) git rm --cached

Answer:

d) git rm --cached

Explanation:

The git rm --cached command is used to remove a file from the staging area without deleting it from the working directory. This command only unstages the file, so it will not be included in the next commit, but it remains in the directory.

For example, if you’ve added a file by mistake, you can use git rm --cached filename to unstage it. The file will still exist locally but won’t be committed until it is re-added.

This command is useful when you want to exclude files from a commit or undo a mistaken git add operation without losing the file entirely.

Reference:

Top 100 Git and GitHub MCQ Questions and Answers

Scroll to Top