Undoing Changes Questions with Answers

8 practice questions on Undoing Changes (Git & DevOps) with answers and step-by-step solutions. Try 8 samples below, then practise all 8 free in the AptiNest app.

Sample questions

Question 1Easy

What is the default mode of git reset <commit> without options?

  1. --mixed: moves the branch and resets the index but keeps the working files
  2. --hard
  3. --soft
  4. --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

  1. Branch pointer moves.
  2. Index resets to that commit.
  3. 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?

  1. Rebase mode
  2. Bare repository
  3. Merge conflict
  4. 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

  1. HEAD holds a hash.
  2. No branch follows new commits.
  3. 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?

  1. Run git log --deleted, which lists every removed branch name
  2. Look in git reflog for its last position
  3. Run git fsck only in a clone
  4. 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

  1. Run git reflog.
  2. Find the line when you were on feat.
  3. 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?

  1. It creates a new commit that undoes the changes of HEAD
  2. It deletes the last commit from history so it no longer appears
  3. It resets the working tree without committing
  4. 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

  1. It computes the inverse patch.
  2. It commits it as a new commit.
  3. 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?

  1. Replaces the working copy of app.py with its version from two commits ago
  2. Resets the whole branch to that older commit and drops later work
  3. Reverts commits automatically
  4. 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

  1. Pick the source commit.
  2. Restore reads that file version.
  3. 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?

  1. Creates a revert commit and keeps the old commit in the history
  2. Keeps the files unstaged
  3. Moves the branch back one commit and discards the changes in index and working tree
  4. 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

  1. Branch moves to the parent.
  2. Index matches it.
  3. 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?

  1. git revert --parent M
  2. git revert --merge M
  3. git revert M^2
  4. 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

  1. Merge has two parents.
  2. Pick which side is the mainline.
  3. -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?

  1. git reset --hard then force push
  2. git revert <commit>
  3. git commit --amend
  4. git rebase --root

Answer: B. git revert <commit>

Explanation

Revert adds a new commit and avoids rewriting shared history.

Step-by-step solution

  1. Shared history should not be rewritten.
  2. Revert adds an inverse commit.
  3. 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 →

Related topics

See all Git & DevOps topics · All topics