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!
Let's try running git log again. 0:00 You've probably noticed a random looking strings of letters and 0:03 numbers that accompany each commit in the log. 0:06 Those strings are actually not random, 0:09 the sting is the SHA1 checksum of all the changes in that commit. 0:12 SHA stands for Simple Hashing Algorithm. 0:16 The checksum is the result of combining all the changes in the commit and 0:20 feeding them to an algorithm that generates these 40 character strings. 0:23 There's no need to worry about the details of that algorithm. 0:28 The part you should remember is that a checksum uniquely identifies a commit. 0:31 Provide git that checksum later, and 0:36 git will be able to find the commit that generated it. 0:38 When you need to select a commit from your history, you can use these SHA1 0:41 checksums or SHAC for short, to identify which commit you want. 0:45 Git revert is just one of many commands that lets you specify a particular commit 0:50 using a SHA. 0:55 With git revert, the SHA is used to specify which commit 0:56 contains the set of changes you want to undo. 0:59 Let's try experimenting with this now. 1:02 I'm going to run git log to show all the commits in our repo's history. 1:04 I'll use the arrow keys to scroll down, 1:10 back to the commit with the message of remove tin medals. 1:12 I'll make note of the SHA from that commit. 1:17 It let's us abbreviate commit SHA. 1:20 So while I could copy and 1:22 paste the whole thing, I can also type just the first five characters or so. 1:23 So I'll remember C02BF, 1:27 I'll press the Q key to exit out of the log and get back to the shell prompt. 1:30 And now, I'll run the git revert command. 1:35 To specify which commit to revert, 1:38 I'll type in the partial SHA from the log as an argument c02bf. 1:40 The revert command works by making a new commit, so 1:43 it will open our editor, so we can edit the commit message. 1:48 It has a default message of revert, 1:53 followed by the commit message from the commit that you're reverting. 1:56 In this case the result is Revert Remove tin metals. 2:01 We'll just keep this default, I'll save the file with Ctrl+O, 2:05 hit Enter to accept the existing file name, and hit Ctrl+X to exit the editor. 2:09 Now let's run the git log command again. 2:14 We'll see a new commit with the default message we saved in our editor, 2:18 Revert Remove tin medals. 2:21 And that commit will have a new SHA. 2:23 Because the commit we selected deleted the tin.html file, 2:26 the revert command makes a commit that adds the tin.html file. 2:30 If we run ls, we'll see that tin.html file. 2:34 And if we reopen the sidebar and refresh the file list, 2:38 we'll see the tin.html file there. 2:44 If we open the editor to look at its contents, 2:47 it looks like those contents are back too. 2:49 Let me just close the editor and hide the sidebar again. 2:52 But after thinking about this some more, 2:56 it's clear that bringing tin medals back is not a good idea. 2:58 Let's run git log again, 3:02 it shows us the commit that re-added the tin.html file right there at the top. 3:04 It's the most recent commit. 3:09 If we revert that commit, the file will be gone again. 3:11 Now, normally, we'd need to make note of the commit SHA so 3:14 we can provide it to the git revert command. 3:17 But because this is the most recent commit, 3:20 there's a special shorthand we can use. 3:22 I'll press Q to quit out of the log, and I'll type git revert HEAD. 3:24 HEAD in all capital letters is a shorthand meaning the most recent commit. 3:32 We could use the full SHA for the most recent commit here or an abbreviated SHA. 3:37 But if we type all caps HEAD, 3:42 git will operate on whichever commit is the most recent one at that time. 3:44 You can use the head short hand in place of a commit SHA in 3:48 any git command that accepts SHA. 3:52 I'll press Enter to run the command and as before, 3:54 git revert will start a new commit that reverts the commit we selected. 3:57 The default message is now revert remove tin medals. 4:01 While that's a complex description of what the commit does, it's also true. 4:06 And since we're just experimenting, I wont bother editing this one either. 4:10 I'll press Ctrl+O to write out the file. 4:14 Press Enter to accept the existing file name and press Ctrl+X to exit the editor. 4:16 And git will complete the revert commit. 4:21 Now if I run git log, 4:23 we'll see another new commit at the top with the default commit message we saved. 4:26 And if I quit the log and 4:30 run ls to get a list of files again, we'll see the tin.html file is gone. 4:32 We can also run git status again, and 4:38 it will show that the removal of the file has already been committed. 4:41 In this second stage, 4:45 we've shown you how to maintain files you've committed to a git repo. 4:47 Practicing your new skills is important to making them stick, so 4:51 be sure to check the teacher's notes on this video for some practice ideas. 4:54 In the third and final stage of this course, 4:59 we'll show you how to share you work by cloning a repo, see you there. 5:01
You need to sign up for Treehouse in order to download course files.Sign up