Git In-depth

Simple Git Commit Solution

Git In-depth

Check out a free preview of the full Git In-depth course

The "Simple Git Commit Solution" Lesson is part of the full, Git In-depth course featured in this preview video. Here's what you'd learn in this lesson:

Nina walks through the Solution for the Simple Git Commit Exercise.

Preview
Close

Transcript from the "Simple Git Commit Solution" Lesson

[00:00:00]
>> Nina Zakharenko: Let's go through the solution. So in this case I'm going to make a new directory and navigate to it. Now if I run git status here, we'll see that this directory doesn't have a git repository associated with it. So I type git in it, that initialized the empty git repository in that directory.

[00:00:37]
Now, let's play around and see what's in that .git directory. So the first thing I'm gonna do is,
>> Nina Zakharenko: Echo,
>> Nina Zakharenko: Hello, World. I'm sorry, have a syntax error here. Gonna echo Hello, World into a file called hello.txt.
>> Nina Zakharenko: Now I'm going to add that file, and I'm going to make a commit.

[00:01:15]
>> Nina Zakharenko: We'll see here that Git has told me that it's created a commit at this hash. Now, this hash will be different for each of you. That's because all the contents of the file are,
>> Nina Zakharenko: The same. The Commit information is different, meaning the author, and the date, and so on and so forth.

[00:01:44]
So you'll all have a different hash for the commit.
>> Nina Zakharenko: Now, in my examples, I was using tree to look inside the .git/objects folder. But if you don't have tree, if you have trouble setting it up, you can just use ls to list files. So for example I can ls the .git/objects directory and it'll show me these two digit prefixes.

[00:02:19]
Now the reason we have these is because huge Git repositories get hundreds of thousands of commits, objects, etc. So Git does a little bit of optimization and it prefixes the first two characters to make a directory. And then any of the files that start with those two characters will be in that subdirectory but missing the prefix.

[00:02:47]
So the hash is hexadecimal, meaning that in a very full Git repository, we might have a maximum of 256 subfolders. But I'm gonna use a tree, because I have it set up. And I'm gonna tree my .git/objects directory. We'll see that we have three new objects. The SHA-1 for your commit might be different than mine.

[00:03:23]
The SHA-1 for your blob will be the same as long as I typed the same thing as you did. And the tree will be the same if your file has the same file name and the same contents as my file, but the commit will be different. So let's check out these files using command git cat file.

[00:03:46]
If I use the -t argument, that'll tell me the type. So the first thing I wanna do is check the first object. And the way that I do that is by typing in the prefix, in this case, 8a, followed by a few characters of the hash. The reason we don't have to type in this whole 40 digit hash every time we're working with Git is that unless your repository is huge, chances are your commits should be addressable by about five characters.

[00:04:22]
Because that's enough differentiation to figure out which commit is unique. If you work on a huge repository and try to use too few characters and there are conflicts, Git will complain and let you know. But chances are none of you are working on the Linux kernel. So, you probably won't run into that problem.

[00:04:42]
So, I'm gonna git cat-file -t, the first file. And I'll see that it's a blob. If I git cat-file -p it. What will I see? Can anyone in the room tell me? What should I see?
>> Speaker 2: Hello, world.
>> Nina Zakharenko: Hello, world, right. So, hello world is the contents of that blob.

[00:05:07]
And the contents of the file hello.txt. Notice here we don't have hello.txt, we don't know what file name this content is associated with. In order to get that information, we're gonna need to look in the tree. So let's do git file -t and check out what the, oops.

[00:05:35]
>> Nina Zakharenko: Let's check out the type of the next object. In this case, it's commit. I wanna look at the tree first. So if I git cat-file -t the last object here, by process of elimination, that should be the tree, cuz our simple repository has one blob, one tree and one commit.

[00:05:59]
And if I git cat-file -p, starting with the prefix bc, what critical information will be in this tree? It's going to be the file name, oops, I am sorry, making typos. All around. Let's clear that and try that again.
>> Nina Zakharenko: So, that's our tree object. it has the file mode, all right.

[00:06:38]
It has blob, meaning it's pointing to a blob. It has that SHA of that blob. So, this SHA here matches this here. And it has that crucial information, the filename hello.txt. Now, let's take a look at our commit. And that is this last object here. If I git cat-file -t, that shows me that it's the commit.

[00:07:20]
Now, if I gtt -file, or git cat-file -p it. It'll show me the information stored in that commit. So it points to the tree that commit points to. You'll see that this SHA here matches this SHA here. The author and the committer and the commit message along with the timestamp.

[00:07:45]
Something crucial to note here, this is our initial commit, so there is no parent. But every other commit that you make in this repository is also going to point to a parent commit. Now, let's take a quick look at the refs. Right now we're on our master branch, it has one commit.

[00:08:10]
So if I cat the value in HEAD, we'll see that it's pointing to the master branch. If I cat the value in .git/refs/heads/master. We'll see that it's pointing to our first commit right here. Note that because head points to master, head also points to this commit right here.

[00:08:47]
So when you're referring to head in this scenario, it will also point to this commit. And we can verify this. I'm gonna run this flag no-pager just so that my log displays in the same screen instead of opening with the page or less.
>> Nina Zakharenko: Oops.
>> Nina Zakharenko: So sorry.

[00:09:25]
--no-pager goes before. There we go. So if I look at my log we'll see our initial commit. And if I look at the master reference, we'll see that this commit matches this commit. These SHAs are the same.
>> Nina Zakharenko: Now,
>> Nina Zakharenko: If we look at our .git/refs directory we will see that our master branch is stored under heads.

[00:10:08]
Now if we create a new branch.
>> Nina Zakharenko: All right, using the git branch commands, not git checkout -b, means that we'll create a new branch without switching to it. So when I run this command, I'm still on the master branch. And to confirm that I can run git status.

[00:10:35]
But now when I use tree to look in my .git/refs directory I'll see that I have a new value under heads, and that's a new branch.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now