Commit SHAs and Undoing Commits5:06 with Jay McGavren
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:
- 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.
git revert command
git revertis just one of many commands that lets you specify a particular commit using a SHA.
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 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 revertcommand, to specify which commit to revert, just type in the partial SHA from the log as an argument:
git revert 1d8e1
revertcommand 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, 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 type
HEAD, Git will operate on whichever commit is the most recent one at that time.
- You can use the
HEADshorthand in place of a commit SHA in any Git command that accepts SHAs.
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.txtfile, but we accidentally ran
git rm f.txt. Unstage the file deletion, then restore the copy in the working directory.
1.txtfile has been committed to the repo. Make a commit that deletes
1.txtfrom the repo.
c.txtfile has accidentally been deleted. Restore the copy in the working directory.
z.txtfile should actually be named
e.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!
You need to sign up for Treehouse in order to download course files.Sign up