Sample questions
Question 1Easy
What is the default mode of git reset <commit> without options?
- --mixed: moves the branch and resets the index but keeps the working files
- --hard
- --soft
- --keep-index
Answer: A. --mixed: moves the branch and resets the index but keeps the working files
Explanation
Mixed unstages changes but leaves the working tree untouched.
Step-by-step solution
- Branch pointer moves.
- Index resets to that commit.
- Files stay as they were.
Why the other options are wrong
- B.
--hard is never the default. - C.
--soft keeps the index too. - D. Not a mode name.
Question 2Medium
After git checkout <commit-hash>, which state are you in?
- Rebase mode
- Bare repository
- Merge conflict
- Detached HEAD
Answer: D. Detached HEAD
Explanation
HEAD points straight to a commit, not a branch. New commits there can be lost unless you create a branch.
Step-by-step solution
- HEAD holds a hash.
- No branch follows new commits.
- Run
git switch -c name to keep them.
Why the other options are wrong
- A. Rebase is a different state.
- B. Bare repos have no working tree.
- C. No conflict is involved.
Question 3Medium
You accidentally deleted a branch feat. How can you find its last commit?
- Run git log --deleted, which lists every removed branch name
- Look in git reflog for its last position
- Run git fsck only in a clone
- Impossible once deleted
Answer: B. Look in git reflog for its last position
Explanation
Reflog records HEAD movements for a time (default ~90 days); recreate with git branch feat <hash>.
Step-by-step solution
- Run
git reflog. - Find the line when you were on feat.
git branch feat <hash>.
Why the other options are wrong
- A. No such option.
- C. It is not necessary.
- D. Commits remain until garbage-collected.
Question 4Medium
Which statement about git revert HEAD is true?
- It creates a new commit that undoes the changes of HEAD
- It deletes the last commit from history so it no longer appears
- It resets the working tree without committing
- It only works on unpushed commits
Answer: A. It creates a new commit that undoes the changes of HEAD
Explanation
Revert is a safe way to undo on shared branches because history is preserved.
Step-by-step solution
- It computes the inverse patch.
- It commits it as a new commit.
- Old commit stays in history.
Why the other options are wrong
- B. That is
reset. - C. Revert commits automatically.
- D. It works on pushed commits too.
Question 5Medium
What does git restore --source=HEAD~2 app.py do?
- Replaces the working copy of app.py with its version from two commits ago
- Resets the whole branch to that older commit and drops later work
- Reverts commits automatically
- Deletes app.py from history
Answer: A. Replaces the working copy of app.py with its version from two commits ago
Explanation
--source picks which commit to read the file from. No commits are changed.
Step-by-step solution
- Pick the source commit.
- Restore reads that file version.
- Working file is overwritten.
Why the other options are wrong
- B. Branch pointers stay.
- C. No revert commits are made.
- D. History is not edited.
Question 6Medium
What does git reset --hard HEAD~1 do?
- Creates a revert commit and keeps the old commit in the history
- Keeps the files unstaged
- Moves the branch back one commit and discards the changes in index and working tree
- Does nothing to files
Answer: C. Moves the branch back one commit and discards the changes in index and working tree
Explanation
Hard reset overwrites the working tree, so uncommitted work is lost. The old commit may still be in reflog.
Step-by-step solution
- Branch moves to the parent.
- Index matches it.
- Working files are overwritten.
Why the other options are wrong
- A. That is
revert. - B. That is
--mixed. - D. Files change.
Tip: Reflog can bring back lost commits, not uncommitted edits.
Question 7Hard
You need to revert a merge commit M. Which option tells Git which parent to keep?
- git revert --parent M
- git revert --merge M
- git revert M^2
- git revert -m 1 M
Answer: D. git revert -m 1 M
Explanation
Merges have several parents, so -m 1 selects the mainline parent whose side is kept.
Step-by-step solution
- Merge has two parents.
- Pick which side is the mainline.
-m 1 is usually the branch merged into.
Why the other options are wrong
- A. No such option.
- B. No such option.
- C. That is a commit reference, not a mainline choice.
Question 8Hard
Which is the safest way to undo a bad commit that is already pushed to a shared branch?
- git reset --hard then force push
- git revert <commit>
- git commit --amend
- git rebase --root
Answer: B. git revert <commit>
Explanation
Revert adds a new commit and avoids rewriting shared history.
Step-by-step solution
- Shared history should not be rewritten.
- Revert adds an inverse commit.
- Push it normally.
Why the other options are wrong
- A. Rewrites shared history.
- C. Amend only edits the last commit and rewrites it.
- D. Rewrites everything.
Practise all 8 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →