Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Git can even undo all the changes from a commit for you, with a command called "git revert".
- You've probably noticed the random-looking strings of letters and numbers shown with each commit in
git log
. For example:
1d8e15a834a2157fe7af04421c42a893e8a1f23a
- Those strings are actually not random; the string is the SHA-1 checksum of all the changes in that commit.
- "SHA" stands for Simple Hashing Algorithm.
- The checksum is the result of combining all the changes in the commit and feeding them to an algorithm that generates these 40-character strings.
- A checksum uniquely identifies a commit.
- When you need to select a commit from your history, you can use these SHA-1 checksums, or "SHAs" for short, to identify which commit you want.
The git revert
command
-
git revert
is just one of many commands that lets you specify a particular commit using a SHA. - With
git revert
, the SHA is used to specify which commit contains the set of changes you want to undo. - You can find a commit you want to undo using
git log
. - Git lets us abbreviate commit SHAs, so while you could copy and paste the whole thing, you can also just remember the first 5 characters or so.
- When running the
git revert
command, to specify which commit to revert, just type in the partial SHA from the log as an argument:
git revert 1d8e1
- The
revert
command works by making a new commit, so it will open an editor so we can edit the commit message. - Save the file and exit to complete the revert commit.
HEAD
-
HEAD
, in all capital letters, is a shorthand meaning "the most recent commit". We could use the full SHA for the most recent commit, or an abbreviated SHA, but if we typeHEAD
, Git will operate on whichever commit is the most recent one at that time. - You can use the
HEAD
shorthand in place of a commit SHA in any Git command that accepts SHAs.
Practice
We've created an alphabet
Git repo that's a little messed up, so you can practice fixing it. Fork this snapshot to get a Workspace containing the repo. Or, if you have Git installed on your computer, you can download this file and unzip it to get a copy of the repo.
Here's what needs fixing:
- We want to keep the
f.txt
file, but we accidentally rangit rm f.txt
. Unstage the file deletion, then restore the copy in the working directory. - The
1.txt
file has been committed to the repo. Make a commit that deletes1.txt
from the repo. - The
c.txt
file has accidentally been deleted. Restore the copy in the working directory. - The
z.txt
file should actually be namede.txt
. Make a commit that moves it to the correct name. - In the Git log, find the commit with the message
"Remove g.txt and h.txt"
, and revert it.
You'll know you're done when running ls
produces this output:
a.txt b.txt c.txt d.txt e.txt f.txt g.txt h.txt
And when running git status
produces this output:
On branch master
nothing to commit, working tree clean
Remember, you can get helpful hints on what command to run next by running the git status
command!
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up