Git Git Undoing Changes 1 β Questions and Answers
Question 1: Which Git command undoes a commit by creating a new commit that reverses the changes?
- git reset
- git revert (Correct answer)
- git restore
- git checkout
Correct answer: git revert
git revert creates a new commit that reverses a previous commit's changes, keeping the original commit in history.
Question 2: What does `git reset --soft HEAD~1` do?
- Deletes the last commit and discards all changes
- Moves HEAD back one commit but keeps changes staged (Correct answer)
- Moves HEAD back one commit and unstages all changes
- Reverts the last commit by creating a new revert commit
Correct answer: Moves HEAD back one commit but keeps changes staged
`--soft` moves the HEAD pointer back one commit but leaves all changes intact in the staging area (index).
Question 3: What does `git reset --hard HEAD~1` do?
- Moves HEAD back one commit and keeps changes staged
- Moves HEAD back one commit and keeps changes in the working directory
- Moves HEAD back one commit and permanently discards all changes (Correct answer)
- Creates a new revert commit that undoes the last commit
Correct answer: Moves HEAD back one commit and permanently discards all changes
`--hard` moves HEAD back and discards both staged and working directory changes, making the operation irreversible without reflog.
Question 4: Which command, introduced in Git 2.23, is the recommended modern way to unstage a file?
- git reset HEAD <file>
- git checkout -- <file>
- git restore --staged <file> (Correct answer)
- git rm --cached <file>
Correct answer: git restore --staged <file>
`git restore --staged <file>` was introduced in Git 2.23 as the dedicated, clearer command to unstage files without the ambiguity of git checkout.
Question 5: Which command discards unstaged changes to a tracked file in the working directory?
- git reset <file>
- git restore <file> (Correct answer)
- git revert <file>
- git clean <file>
Correct answer: git restore <file>
`git restore <file>` discards working directory changes for a tracked file, restoring it to its last committed (or staged) state.
Question 6: What is the key difference between `git reset --mixed` and `git reset --soft`?
- --mixed preserves staged changes while --soft discards them
- --mixed unstages changes but keeps them in the working directory while --soft keeps them staged (Correct answer)
- --mixed deletes working directory changes while --soft keeps them staged
- There is no functional difference between the two modes
Correct answer: --mixed unstages changes but keeps them in the working directory while --soft keeps them staged
`--mixed` (the default) moves HEAD and unstages changes, placing them back in the working directory, while `--soft` moves HEAD and keeps everything staged.
Question 7: Which command removes untracked files from the working directory?
- git reset --hard
- git restore .
- git clean -f (Correct answer)
- git rm -r .
Correct answer: git clean -f
`git clean -f` removes untracked files from the working directory; the `-f` (force) flag is required by default as a safety measure.
Which Git command undoes a commit by creating a new commit that reverses the changes?