Undoing Changes — reset, revert, restore, stash — Concept Check
Questions on Undoing Changes — reset, revert, restore, stash. Git has four main undo mechanisms for different situations: restore (discard working changes), reset (move HEAD and optionally unstage/discard), revert (create a new commit that undoes a past commit), and stash (temporarily shelve work).
Questions in this quiz
8 questions on Undoing Changes — reset, revert, restore, stash — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.You have made changes to several files in your working directory but decide you want to discard all of them. Which Git command should you use?
- Agit revert HEAD
- Bgit reset --soft HEAD
- Cgit stash
- Dgit restore .
2.A developer wants to undo a commit but keep the changes in the staging area for a quick recommit. Which command should they use?
- Agit stash
- Bgit revert HEAD
- Cgit reset --soft HEAD~1
- Dgit reset --hard HEAD~1
3.You accidentally committed changes to a shared branch and need to undo them without rewriting history. What should you do?
- AUse 'git stash' to temporarily remove the commit.
- BUse 'git restore' to discard the commit.
- CUse 'git reset --hard' to remove the commit.
- DUse 'git revert' to create a new commit that undoes the changes.
4.A developer uses 'git reset --hard HEAD~1' on a local branch. What happens to the changes in the working directory?
- AThey remain staged.
- BThey are moved to the stash.
- CThey are permanently discarded.
- DThey are kept in the working directory.
5.You want to temporarily save your current changes, including untracked files, to switch branches. Which command should you use?
- Agit revert HEAD
- Bgit stash push -u
- Cgit reset --mixed
- Dgit stash apply
6.A developer thinks using 'git reset --hard' is a safe way to undo changes on a shared branch. Why is this incorrect?
- AIt creates a new commit instead of undoing the previous one.
- BIt only affects the staging area, not the commit history.
- CIt stashes changes instead of discarding them.
- DIt rewrites history and discards changes permanently, which is unsafe for shared branches.
7.Which command should you use to unstage a file but keep its changes in the working directory?
- Agit restore --staged <file>
- Bgit revert <commit>
- Cgit reset --hard HEAD
- Dgit stash pop
8.What is the role of 'git stash' in managing changes?
- AIt creates a new commit that undoes previous changes.
- BIt moves the HEAD pointer to a previous commit.
- CIt temporarily saves changes to a stack for later use.
- DIt permanently discards changes from the working directory.