XK0-004 · Question #187
An issue was discovered on a testing branch of a Git repository. A file was inadvertently modified and needs to be reverted to the master branch version. Which of the following is the BEST option to r
The correct answer is D. git checkout master -- file. To revert a single file in a working branch to its state from another branch, git checkout with a branch specifier and file path is the precise tool.
Question
An issue was discovered on a testing branch of a Git repository. A file was inadvertently modified and needs to be reverted to the master branch version. Which of the following is the BEST option to resolve the issue?
Options
- Agit branch -b master file
- Bgit merge master testing
- Cgit stash branch master
- Dgit checkout master -- file
How the community answered
(43 responses)- A2% (1)
- B5% (2)
- C9% (4)
- D84% (36)
Why each option
To revert a single file in a working branch to its state from another branch, git checkout with a branch specifier and file path is the precise tool.
`git branch -b` is not valid syntax; the flag for creating and switching to a new branch is `-b` used with `git checkout`, and it creates a branch rather than restoring a file.
`git merge master testing` merges the entire master branch history into testing, which would affect all files, not just the one inadvertently modified.
`git stash branch master` creates a new branch from a stash entry; it does not restore a file from an existing branch.
The command `git checkout master -- file` instructs Git to restore the specified file to the exact version found in the master branch, leaving all other files in the testing branch untouched. The double-dash `--` explicitly separates the branch name from the file path, preventing ambiguity. This is the standard, targeted way to recover a single file from another branch without affecting the rest of the working tree.
Concept tested: Restoring a single file from another Git branch
Source: https://git-scm.com/docs/git-checkout
Topics
Community Discussion
No community discussion yet for this question.