Check out a free preview of the full Everything You'll Need to Know About Git course
The "Diving into the Commit SHA" Lesson is part of the full, Everything You'll Need to Know About Git course featured in this preview video. Here's what you'd learn in this lesson:
ThePrimeagen walks through the process of finding and accessing a specific commit in the Git folder. Students are encouraged to use the "find" command to locate the commit and then use the "cat-file -p" command to view the contents of the commit. He also emphasizes that all of Git's state is stored in these files and demonstrates how to use the command to access the contents of a specific file within the commit.
Transcript from the "Diving into the Commit SHA" Lesson
[00:00:00]
>> All right, so the next part will be a little bit hard, but I think you can try to give it a shot. Do you think you can find your SHA in the GIT folder? Why don't you take a look, go in there. Remember the SHA is what you're looking for.
[00:00:13]
Just do a little perusing on your own for a minute. I'll give you another hint. The first two characters of the sha are important. Look around, maybe try to find command finds a great one, it will list out a bunch of files for you. All right, I'll just kind of start doing it.
[00:00:32]
So if I go like this find, remember when I did it before, there wasn't a ton of files, but if I do it again, notice now we have a whole bunch of stuff inside of objects now. And not only that, but there's my 8/7. There's my 2/1/a. Look at that thing.
[00:00:47]
That's my commit, right? That looks like my commit. You have your commit somewhere in there and you can go and find it. It's a real thing, right? Little quick observation. Everything inside the git.objects. Or sorry, .get/objects directory will have the first two letters of any any Sha that is being tracked by Git.
[00:01:06]
And you can use that to find stuff. If you're to manually look through Git, all right? That's where they're all stored. That's where all of your changes and everything are stored. So, by the way, I absolutely love is like my favorite part of The Hobbit was what sort of it's Pocketses?
[00:01:21]
I just love that part. For those that don't know, it's a fantastic scene where Bilbo has the ring and needing to ask riddles. And if he gets a riddle wrong, he gets eaten, and his last one isn't a riddle. He just said, what's in my pocket? Then Gollum goes crazy, and that's the whole story.
[00:01:36]
That's how Lord of the Rings happened, is because he wouldn't tell him what's in his pocketses. It's crazy, anyways. All right, so if I were to go here and say git/objects/8/7/ and cap this thing out. It's a bunch of nonsense. You can do the same thing, right? You can't understand that file, or this commit from just catting stuff out.
[00:01:55]
It's been compressed, so it's more space efficient. So it's just nonsense to our eyes. Here's my version right here. Remember, all of git state is stored in these files. All of it, so that's why our commit's there. All right, so some of the tools of a plumber. There's a bunch of different ways you can manually use the right git command to print out the contents of stuff.
[00:02:20]
But there's also Cat-File-p, and that will just print out any SHA you want and the content or in the contents of that SHA. And so there are like, if you actually use the actual get commands, there's like a tree one for printing out trees. There's one for printing out specific parts or just this one.
[00:02:36]
I prefer just to use this, because it's kind of annoying to have to know all the little sub commands. I'm sure there's like a cost to it, which is like Git has to figure out which SHA you're actually talking about and the type of SHA and then to compress it and to give you this nice view and all that, but just use this.
[00:02:53]
You can inspect any commit or anything about Git by using git cat-file-p. So I want you to cat file your sha. And see if you can get to the point of being able to get the contents out of first MD. See if you can actually get all the way down and find it by just cutting out your sha.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops