Skip to Content
Learn
How to Backtrack
git reset II

Creating a project is like hiking in a forest. Sometimes you take a wrong turn and find yourself lost.

Just like retracing your steps on that hike, Git enables you to rewind to the part before you made the wrong turn. You can do this with:

git reset commit_SHA

This command works by using the first 7 characters of the SHA of a previous commit. For example, if the SHA of the previous commit is 5d692065cf51a2f50ea8e7b19b5a7ae512f633ba, use:

git reset 5d69206

HEAD is now set to that previous commit.

Instructions

1.

From the terminal, print out your Git commit log.

Note: If your cursor gets stuck in “git log” mode in the terminal, press “q” on your keyboard to escape.

2.

From the terminal, enter the command to reset to a previous commit, using the first 7 characters of one of the past commit SHAs in your Git log.

Next, print the Git commit log again.

Notice anything interesting? The commits that came after the one you reset to are gone. The HEAD commit has been reassigned. You just changed history.

Folder Icon

Sign up to start coding

Already have an account?