350-901 practice question 19 of 35

A developer has issued git add file1 and file2 test.py command to add the three files for the next commit, but then decides to executed test.py from this command. Which command needs to be used to exclude test.py from this commit but keep the rest of the files?

Full set: 430 questions

Domain Using APIs

Answer options
A: git clean — test.py
B: git reset - test py Correct answer
C: git checkout - file1 file2
D: git stash -- file1 file 2
Correct Answer:
B. git reset - test py
Explanation

The git add command moves changes from the working directory to the staging area (index). To reverse this action for a specific file—that is, to "unstage" it—the git reset command is used. This command removes the specified file from the staging area but preserves the modifications in the working directory. In this scenario, git reset test.py (or more explicitly, git reset HEAD test.py) will unstage test.py, leaving file1 and file2 staged and ready for the next commit.

Why Incorrect

A. git clean — test.py: This command is incorrect because git clean is used to remove untracked files from the working directory, not to unstage tracked files.
C. git checkout - file1 file2: This command is incorrect as git checkout -- discards changes in the working directory, reverting the file to its last committed state.
D. git stash -- file1 file2: This command is incorrect. git stash is used to temporarily store all modified tracked files, not to selectively unstage a single file.

References

1. Official Git Documentation for git-reset: "When is given

git reset updates the index (i.e. the staging area) with the contents of the files from (which defaults to HEAD)

without touching the files in the working tree." This describes the exact process of unstaging a file.

Source: git-scm.com

Documentation for git-reset

git-reset(1) Manual Page

section "SYNOPSIS" (git reset [] [--] ...).

2. Pro Git (Official Book): "How do you unstage a file? ... As it turns out

git reset HEAD does the trick. Let’s use that command to unstage the CONTRIBUTING.md file: $ git reset HEAD CONTRIBUTING.md".

Source: Chacon

S.

& Straub

B. (2014). Pro Git (2nd ed.). Apress. Chapter 2.3

"Git Basics - Recording Changes to the Repository

" section "Unstaging a Staged File."

3. MIT OpenCourseWare: In the lecture on version control

the process of unstaging is explained. "To unstage a file

i.e. do the opposite of git add

you can use git reset ."

Source: MIT

"The Missing Semester of Your CS Education" (2020). Lecture 6: Version Control (Git). missing.csail.mit.edu/2020/version-control/.

Scroll to Top