How do you undo changes in the working directory that haven’t been committed yet?

How do you undo changes in the working directory that haven’t been committed yet?

a) git revert
b) git reset --hard
c) git checkout -- file
d) git delete

Answer:

c) git checkout -- file

Explanation:

The command git checkout -- file is used to undo changes in the working directory for a specific file, reverting it to the state from the last commit. It does not affect staged changes, only changes in the working directory.

For example, if you’ve made changes to file.txt but haven’t committed them yet, running git checkout -- file.txt will discard the modifications.

This is useful when you want to revert uncommitted changes and reset the file to its previous state.

Reference:

Top 100 Git and GitHub MCQ Questions and Answers

Scroll to Top