2.3 Making Commits
Finally, we’re doing what Git was intended for! In this video, we’ll our first commit.
1.Introduction3 lessons, 19:10
2.Basic Commands4 lessons, 23:13
3.A Bit of Theory2 lessons, 23:37
4.Logs and Branches4 lessons, 33:30
5.Github and Remotes4 lessons, 41:49
6.Intermediate Usage6 lessons, 41:55
7.Conclusion1 lesson, 01:07
2.3 Making Commits
So we're finally ready to actually start committing changes to our git repository. So let's go ahead and open up our terminal and move into our project directory. And I'll begin by running a git status just to see where we are, so we're right where we left off. We've got two files that we created, and we staged all of the changes in those files, so we're ready to commit our README and our todo files. And to begin with, the simplest way to do a git commit is just to run git commit and when I hit enter, we're taken into an editor and for for me my editor of choice is vim. And so now, we've got a bunch of stuff down below here which looks very much like the output we would get from a git status call. And we've got a message here at the top, let's read that. It says, please enter the commit message for your changes. Lines starting with a # will be ignored, and an empty message will abort the commit. So, as you can see, this output here all begins with a hash mark, or the number sign, which means it will all be ignored. It will not be part of the committing message. It's just there to fill us in on what is actually going into this commit. And as we can see, the changes to be committed are the two new files that we created. So right at the top, we've got a single empty line, and I could actually take up as many empty lines as I want here. What we're doing right now is we're writing a message, to go with the commit. Every commit has a message to kind of explain what is in the commit. So I'm just going to call this initial commit, and then we'll just say created README and TODO files. And when I save and quit the editor, the commit is made. And as you can see right here, we've got this output right here. So we have master, root commit, a couple of letters there which don't make much sense, initial commit, created README and TODO files. So that's our commit message right there, and then down here, we can see what actually went into it, so two files were created, and nine insertions. That just basically means nine lines of code, or in our case, text, were inserted, and so we can see these two files were created down below. So what does some of this beginning stuff here mean? Well, you don't have to worry about the first part, but this right here is the beginning of a hash. We'll talk more in a future video about how git creates this hash. But if you're familiar which much programming at all, you've probably seen MD5 hashes or SHA-1 hashes, which is just basically a way to get a unique 40 character string for a bunch of content. That's what this is, this is the first, what is this, 7 letters of a 40 character string. Which is the SHA-1 hash for the commit that we just made. Every commit will have a SHA-1 hash and since the things are gonna be changed from commit to commit, every SHA-1 hash, the hash for each commit is a unique identifier. So, we've just made our first commit. Now, if we run get status, you can see that we have the text nothing to commit. The working directory is clean. When we say working directory is clean, that just means, there are no changes have been made that have not been already staged and committed. So, that seems pretty straightforward enough. However, we can actually do a few things that will speed up our commit process. For example, let's make a new file here. I'm gonna make make new file and we'll call it license. And we'll just say, this is a license for our project. I'll save and close that, and let me clean up my screen here, clear my screen, and then when I ran git status, you can see that we have another an untracked file here. Let me also make a change to our todo file. We'll add a second todo here which says, learn about ignoring files. And let's just put a mark here that says, done. So I'll save and close that. And now if we run git status, we have an untracked file. And we have a modification that has not been staged. So now let's learn about a couple of flags that we can use, with the git commit command to make committing a bit quicker. One thing you're gonna want to do often is just stage the changes from files that you have previously staged, or just staged changes from files that git is tracking. So in our example here, we would want to stage the modified todo file and the changes modified in there. But we do not want to stage license file, just yet. So what we can do is use the dash A flag, currently nothing has been staged. But we can skip the process of staging those files, by saying, git commit -a. And dash A is just gonna stage any changes made to files that have been previously committed. So I hit that, I'm taken to our editor as before to write our commit message, but notice that it doesn't say we have changes that are not staged. It says changes to be committed. And we've modified todo and then the untracked file's license. So the changes that were made to the todo file have been staged, because we used the -a flag. We staged all the files that were previously committed. So here, I can just say, updated todo file. And when I save and quit that, you can see down here we have a commit message updated todo file, we have our unique hash, and one file changed. Two lines were inserted. One line was deleted. That is what was changed in that commit. Now, if I run git status, you can see that our license file is the only thing that has not been committed. Okay, there's one more flag that we can learn to make committing even quicker. First, I'm gonna say, git add the license file, and so now if I run get status, we have our new file, and change that when we commit it. And now I'm gonna run git commit, except I'm gonna say -m. And dash M means, the string that follows this will be the commit message, so you don't have to open up the editor. So I can just say here, added licence file, and now, notice that when I hit Enter, we're not taken away to the editor. Git recognized that -m means, we're passing the message right here, and you can see our commit message there. Is the same as this commit message here. One file was changed, one insertion, and we created the license file in the repository. And so now, if I run git status, we're clean. The working directory is clean, which means there's nothing to commit. So those are two very handy flags that you will use when committing files often. And I find is that as I'm working whenever I'm committing, very often I'm saying, git commit -am which just combines both flag and then message here just like that. And since there's nothing to commit, we didn't actually make a commit there. If we had changed files, that were previously committed. This allows us to skip the staging part and just stage everything and commit it. Now of course sometimes you don't always want to stage everything. But often you've got a small number of files you're working on. You make a change. You commit it. You add another feature, you commit it. Something like that. And this just allows you to tighten up your git workflow as much as possible. So there you go. Now you know one of the most important parts of git. How to make a commit.