nerdexam
CompTIA

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.

Scripting, Containers and Automation

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)
  • A
    2% (1)
  • B
    5% (2)
  • C
    9% (4)
  • D
    84% (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.

Agit branch -b master file

`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.

Bgit merge master testing

`git merge master testing` merges the entire master branch history into testing, which would affect all files, not just the one inadvertently modified.

Cgit stash branch master

`git stash branch master` creates a new branch from a stash entry; it does not restore a file from an existing branch.

Dgit checkout master -- fileCorrect

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

#Git#git checkout#branch management#file revert

Community Discussion

No community discussion yet for this question.

Full XK0-004 Practice