Undoing Changes: Reset, Revert, and Checkout Quiz Quiz

Explore essential concepts for undoing changes in Git, focusing on the differences and use-cases of reset, revert, and checkout commands. This quiz challenges you to identify correct scenarios and effects when managing change history within version control workflows.

  1. Purpose of git reset

    When using 'git reset --hard HEAD~1', what is the primary effect on your repository's history and working directory?

    1. It undoes the last commit while preserving the working directory changes.
    2. It permanently moves the current branch pointer back by one commit and deletes changes in the working directory.
    3. It stages all changes without affecting any commits.
    4. It creates a new commit that reverses the last commit.

    Explanation: The 'git reset --hard HEAD~1' command erases the last commit and forces your working directory and index to match the new branch tip, discarding any unstaged and staged changes. The second option only describes a soft reset, which does not affect the working directory. The third option refers to 'revert', which actually creates a new commit. The fourth option relates to adding or staging changes, not resetting.

  2. Main function of git revert

    Which statement best describes what happens when you run 'git revert b6f1d91'?

    1. It switches your working directory to the state of commit b6f1d91.
    2. It creates a new commit that undoes the changes made by commit b6f1d91.
    3. It deletes the commit b6f1d91 from history permanently.
    4. It stages the changes introduced by b6f1d91 for commit.

    Explanation: 'git revert' is used to create a new commit that reverses the effect of a specific commit, preserving project history. It does not delete any commits, making option two incorrect. Option three is closer to what 'checkout' does, not revert. Option four describes staging, which is not part of revert's functionality.

  3. Behavior of git checkout on files

    If you run 'git checkout -- README.md', what outcome should you expect in your working directory for that file?

    1. README.md is renamed to README_old.md in your working directory.
    2. README.md is permanently deleted from the project.
    3. Changes to README.md are staged for the next commit.
    4. All changes to README.md since the last commit are discarded and it reverts to the committed state.

    Explanation: The 'git checkout -- README.md' command overwrites the file in the working directory with the version from the last commit, discarding any local modifications. The file is not deleted, so option two is inaccurate. Option three describes a renaming action not performed by checkout. Option four refers to staging, which is typically done by 'git add'.

  4. Correct use-case for git reset vs revert

    In a collaborative environment, which situation is most appropriate for using 'git revert' instead of 'git reset'?

    1. When you wish to move your branch pointer backwards before pushing to remote.
    2. When you intend to switch temporarily to another branch to test code.
    3. When you want to permanently remove untracked files from your directory.
    4. When you need to undo a shared commit on the main branch without rewriting history.

    Explanation: 'git revert' is best in collaborative workflows to undo changes without affecting project history, making it safe for shared branches. Using 'reset' on shared branches can cause problems for others by rewriting history, which option two incorrectly suggests. Removing untracked files is achieved with cleaning commands, not revert or reset, making option three incorrect. Option four actually refers to 'checkout', not revert.

  5. Effect of git reset --soft

    What does running 'git reset --soft HEAD~2' do to your current branch and staged changes?

    1. Deletes the last two commits and permanently removes the associated files.
    2. Creates two new commits that reverse the previous two commits.
    3. Switches your working directory to a new branch named HEAD~2.
    4. Moves the branch pointer back by two commits and preserves all changes as staged for commit.

    Explanation: 'git reset --soft HEAD~2' only moves the branch pointer, keeping all changes from the undone commits staged and ready for recommit. It does not delete files, which makes the second option untrue. The third option is what 'git revert' does, not reset. The fourth option suggests branch manipulation, which is unrelated to this command.