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