What is the command to undo the last commit in Git?

What is the command to undo the last commit in Git?

a) git reset --hard HEAD^
b) git revert HEAD
c) Both a and b
d) git delete HEAD

Answer:

c) Both a and b

Explanation:

In Git, you can undo the last commit using either git reset --hard HEAD^ or git revert HEAD. The git reset command removes the commit and changes, while git revert creates a new commit that undoes the changes made by the last commit.

For example, git reset --hard HEAD^ moves the HEAD pointer to the previous commit and discards all changes. On the other hand, git revert HEAD is safer because it doesn’t alter the commit history, making it ideal for shared branches.

Understanding both methods is essential for correcting mistakes in Git while preserving the integrity of your project’s commit history.

Reference:

Top 100 Git and GitHub MCQ Questions and Answers

Scroll to Top