Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
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.
You've probably noticed a random looking strings of letters and
numbers that accompany each commit in the log.
Those strings are actually not random,
the sting is the SHA1 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.
There's no need to worry about the details of that algorithm.
The part you should remember is that a checksum uniquely identifies a commit.
Provide git that checksum later, and
git will be able to find the commit that generated it.
When you need to select a commit from your history, you can use these SHA1
checksums or SHAC for short, to identify which commit you want.
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.
Let's try experimenting with this now.
I'm going to run git log to show all the commits in our repo's history.
I'll use the arrow keys to scroll down,
back to the commit with the message of remove tin medals.
I'll make note of the SHA from that commit.
It let's us abbreviate commit SHA.
So while I could copy and
paste the whole thing, I can also type just the first five characters or so.
So I'll remember C02BF,
I'll press the Q key to exit out of the log and get back to the shell prompt.
And now, I'll run the git revert command.
To specify which commit to revert,
I'll type in the partial SHA from the log as an argument c02bf.
The revert command works by making a new commit, so
it will open our editor, so we can edit the commit message.
It has a default message of revert,
followed by the commit message from the commit that you're reverting.
In this case the result is Revert Remove tin metals.
We'll just keep this default, I'll save the file with Ctrl+O,
hit Enter to accept the existing file name, and hit Ctrl+X to exit the editor.
Now let's run the git log command again.
We'll see a new commit with the default message we saved in our editor,
Revert Remove tin medals.
And that commit will have a new SHA.
Because the commit we selected deleted the tin.html file,
the revert command makes a commit that adds the tin.html file.
If we run ls, we'll see that tin.html file.
And if we reopen the sidebar and refresh the file list,
we'll see the tin.html file there.
If we open the editor to look at its contents,
it looks like those contents are back too.
Let me just close the editor and hide the sidebar again.
But after thinking about this some more,
it's clear that bringing tin medals back is not a good idea.
Let's run git log again,
it shows us the commit that re-added the tin.html file right there at the top.
It's the most recent commit.
If we revert that commit, the file will be gone again.
Now, normally, we'd need to make note of the commit SHA so
we can provide it to the git revert command.
But because this is the most recent commit,
there's a special shorthand we can use.
I'll press Q to quit out of the log, and I'll type git revert 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 here or an abbreviated SHA.
But if we type all caps HEAD,
git will operate on whichever commit is the most recent one at that time.
You can use the head short hand in place of a commit SHA in
any git command that accepts SHA.
I'll press Enter to run the command and as before,
git revert will start a new commit that reverts the commit we selected.
The default message is now revert remove tin medals.
While that's a complex description of what the commit does, it's also true.
And since we're just experimenting, I wont bother editing this one either.
I'll press Ctrl+O to write out the file.
Press Enter to accept the existing file name and press Ctrl+X to exit the editor.
And git will complete the revert commit.
Now if I run git log,
we'll see another new commit at the top with the default commit message we saved.
And if I quit the log and
run ls to get a list of files again, we'll see the tin.html file is gone.
We can also run git status again, and
it will show that the removal of the file has already been committed.
In this second stage,
we've shown you how to maintain files you've committed to a git repo.
Practicing your new skills is important to making them stick, so
be sure to check the teacher's notes on this video for some practice ideas.
In the third and final stage of this course,
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