‹ All Git Interview Questions questions

Git Interview Question 2: Reset Soft Squash

Git Interview Questions · Test 1 · question 2 of 10

Undoing Changesmedium

On your local feature/search branch you have made three commits named wip, fix typo, and wip 2. None of them has been pushed. Before you open a pull request you want to replace them with one commit that contains exactly the same final file contents. Your working tree is clean.

Which sequence achieves this?

Select one answer
Show answer and explanation

Answer:

  • C. git reset --soft HEAD~3, then git commit -m "Add search"

git reset --soft HEAD~3 moves the branch pointer back three commits and leaves both the index and the working tree unchanged. The combined changes from the three commits therefore stay staged, and one git commit records them as a single new commit. The Pro Git book uses this exact technique for squashing.

Option A is destructive. --hard also overwrites the index and working tree with the contents of HEAD~3, so the work from all three commits disappears from your files and nothing is left to commit. You would need the reflog to get it back.

Option B keeps the file contents on disk, but --mixed (the default mode) resets the index to match the new HEAD, so nothing is staged. The git commit that follows has nothing to record. You would first need to git add the changes again.

Option D does the opposite of what you want. git revert creates new commits that undo the changes, so the branch would end with the three original commits plus three revert commits, and the feature would be gone.

References: git-reset; Pro Git: Reset Demystified

Practice Test 1 ›

Study guides for Git Interview Questions