Check out a free preview of the full Everything You'll Need to Know About Git course
The "Basics & 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 explains the basic Git commands of add, commit, and status. He demonstrates how to create a file, add it to the staging area, commit it with a message, and check the status of the repository. The Git log command and how to use the options --graph and --decorate to view the commit history are also demonstrated. The concept of SHA (Secure Hashing Algorithm) and how it is used to uniquely identify commits is also discussed in this lesson.
Transcript from the "Basics & Commit SHA" Lesson
[00:00:00]
>> This is pretty much the 80% of you'll do with Git. If this is all you ever learn about Git, you'll be moderately successful. Like you can get pretty dang far knowing about Git add path to file or a file pattern, Git commit dash M and Git status.
[00:00:16]
If you can just know those three commands, you're already doing pretty dang good, okay, okay? So get a Git add will stage the file, it will add it to the index. Git commit will take your staged file, index file, and then turn it into a commit, with your author name, the time, the contents of the change, all that and we'll produce out a SHA.
[00:00:36]
Did you just know that SHA stands for Secure Hashing Algorithm? Isn't that kinda disappointing? It's a variant of MD5. Either way, it is always disappointing to know that SHA means Secure Hashing Algorithm. And then Git status will just say, here's the state of your repo. All right, problem.
[00:00:55]
We want to trace the steps of Git from untrack to tracked. Don't forget these ones right here. This is what I want you to do. I want you to create a file called first.md. I want you to check the status of Git. Then I want you to add first.md to the staging area, recheck the status, see that it's being tracked, commit it with a friendly message, and then check the status again.
[00:01:22]
If you're pretty good with Git already, you already know these basic commands, please just create a commit really quickly with first.md. We're gonna use it and we're gonna do something. All right, I'll start doing it right now. So I'm gonna jump over here. I'm gonna go to first.md, I'll go hello fam, there we go, Git status, you'll see right here, untracked file.
[00:01:42]
Git has no idea anything about this file. First time it's ever seen it. It doesn't have any information about it. If I go Git add this, and then redo it, you'll see now we are tracking it. It's officially a part of the Git ecosystem, but only on a singular level, meaning that it's only a part of the staging, it has no prior information besides for staging.
[00:02:04]
If I go get commit a- m, friendly message, look how nice that is. It's gonna tell me, hey, this is the first commit of the repo, the root commit, it has no parents. Typically, I usually go with Batman for my first commit. I think it's funny cuz Batman doesn't have any parents.
[00:02:19]
It's like you gotta always go for that on the first repo, I think it's fantastic. And there you go. So we've made our first commit. So that means if I go Git status, says right here, what branch you're on, nothing to commit. There you go. The tree is clean.
[00:02:35]
The changes are in the repo. One of the common activities you should get into the habit of or at least notes available is Git log. Git log allows you to look at the history of the repo. It's a very powerful thing and we're gonna go over a few of the options today, and we're gonna use it very regularly throughout this course.
[00:02:56]
Cuz it's just awesome to have in your Batbelt, I guess we're going with Batman themed things. Anyways, so reviewer explore, check out, man Git log, this I want you to open up the man page and actually look at it. And I want you to search for-- graph and-- decorate, and just kind of do a quick read, get kind of used to reading the manual.
[00:03:18]
I'll do it along with you, man Git log. There's graph right here and if I find the option, there's the draw text based graphical representation of the commit history. Okay, good to know we have that. All right, and then we can go decorate, let's find the option, may have to go backwards, decorate often is a little bit harder to read.
[00:03:42]
It prints out the rough names of any commits that are shown. Probably not really sure what a ref name is. Remember, everything's a commit, everything has a SHA, and you can reference those commits with some sort of named item branches, right? Branches, tags, there you go. Or the very special one named head.
[00:04:02]
We'll go over ahead in a little bit. All right, problem. Display the history of your repo with graph and decorate. So I'll do it right now. Git log, graph, decorate. Often you don't actually have to use decorate if it's just printing out the standard in. Kinda like a little fun exercise to do.
[00:04:24]
Watch this. If I go this, I drop decorate and I just do graph. You'll notice right here, head points to master. If I take this and put it out to a file and then cat out the file, look at what's missing. That bit of information. So, if you're ever, printing out anything to a file, you need decorate to actually get decorate.
[00:04:48]
You need that flag to tell you, hey, list out all the named commits for me. Nice little, just nice little thing. Thanks so often you don't use decorate like in practice because you're just looking at the log, just something to be aware of. Unfortunately, we only have one commit.
[00:05:06]
So the graph option really isn't like super fantastic, right? You're not actually seeing a graph, there's nothing to graph. But you'll notice that there's like this little asterix right here. And that little asterik is your only node in the graph. So that's the beginning of the graph, item when you use log.
[00:05:23]
We'll look at it a lot more. All right, hopefully everybody's on the same page at this point. So we're gonna now get into some of the get internals, right? We're going from 0 to 15 and then we're just gonna jump right in at like 200 miles an hour.
[00:05:35]
So we're gonna go all the way in. Again, SHAs, they're just a 0 through 9, A through F string of characters 40 of them. And so now we're gonna do the plumbing part. I'm very excited. By the way you can specify the first seven characters of any SHA, and get will resolve that out.
[00:05:53]
So you just need to get at least the first seven characters. You can use log to be able to get out your SHAs. There's other ways to get out SHAs, but log is the simplest way. You don't have to know this one special command for getting out SHAs and said, you can just use log.
[00:06:09]
It's a problem. Find the first commit SHA of your first commit. And I want you to copy it to your system clipboard. This is the important part. Everybody has to do this, okay? Cuz now we're doing the internal. So get the SHA. So me, I'll go get log, and I can come up here and there we go.
[00:06:31]
So I have it nicely on my clipboard right now. Quick question. Why is your SHA different than mine? Mine is 8721A, yours is probably not any of that. Why is it different?
>> Different inputs. Not just based on the like one markdown file, but also your l user not named, I'd email, maybe other environmental bars.
[00:06:57]
>> Exactly. So you pretty much got the idea. My SHA is based off what I committed, the time of the day, my username, my email. So I should produce a unique one comparatively to you. So if we both at the exact same time of the day, both committed the same thing, it should produce different SHAs.
[00:07:14]
Cuz it's an amalgamation of a bunch of different options. Just so if you're wondering, you can't follow along directly, you won't be able to just use the SHA that's produced, you'll have to be able to grab out your own.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops