What is the difference between git reset --hard HEAD~1 and git revert HEAD?

Prepare for the TJR Bootcamp Test with targeted questions and detailed explanations. Use mock exams to enhance understanding and boost your confidence. Gear up for success!

Multiple Choice

What is the difference between git reset --hard HEAD~1 and git revert HEAD?

Explanation:
When you undo changes in Git, you have to choose between altering history or preserving it. Resetting the branch with hard resets the current HEAD back to a previous commit, specifically HEAD~1 in this case, and makes the working directory and staging area match that earlier state. That means the most recent commit is effectively removed from the branch’s history, as if it never happened. This is a history rewrite and can cause problems for others if the changes were already shared, since you’d typically need to force-push to update the remote. Reverting, on the other hand, does not remove any commits from the history. Instead, it creates a new commit on top of the current branch that applies the opposite changes of the target commit, effectively undoing its effects while leaving the original commit in the history. This preserves the project’s history and is safer for shared branches. So the best description is that the reset option changes history and discards commits, while revert adds a new commit that undoes the changes. The other statements don’t fit because they either imply the history is unchanged or suggest that revert deletes commits, which it does not.

When you undo changes in Git, you have to choose between altering history or preserving it. Resetting the branch with hard resets the current HEAD back to a previous commit, specifically HEAD~1 in this case, and makes the working directory and staging area match that earlier state. That means the most recent commit is effectively removed from the branch’s history, as if it never happened. This is a history rewrite and can cause problems for others if the changes were already shared, since you’d typically need to force-push to update the remote.

Reverting, on the other hand, does not remove any commits from the history. Instead, it creates a new commit on top of the current branch that applies the opposite changes of the target commit, effectively undoing its effects while leaving the original commit in the history. This preserves the project’s history and is safer for shared branches.

So the best description is that the reset option changes history and discards commits, while revert adds a new commit that undoes the changes. The other statements don’t fit because they either imply the history is unchanged or suggest that revert deletes commits, which it does not.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy