One Last Git Status Check
If you haven’t added any new files to the Working Directory or modified any of the existing files, nothing will have changed, but to make sure, let’s run a quick git status
again right before we make the commit just to make absolutely sure the project is how we left it.
Make A Commit
Ok, let’s do it!
To make a commit in Git you use the git commit
command, but don't run it just yet. Running this command will open the code editor that you configured way back in the first lesson. If you haven't run this command yet:
$ git config --global core.editor <your-editor's-config-went-here>
…go back to the Git configuration step and configure Git to use your chosen editor.
If you didn’t do this step and you already ran git commit
, then Git probably defaulted to using the "Vim" editor. Vim is a popular editor for people who have been using Unix or Linux systems forever, but it's not the friendliest for new users. It's definitely not in the scope of this course. Check out this Stack Overflow post on how to get out of Vim and return to the regular command prompt.
If you did configure your editor, then go ahead and make a commit using the git commit
command:
Remember, your editor should pop open and you should see something like this:
Terminal Hangs
If you switch back to the Terminal for a quick second, you’ll see that the Terminal is chillin’ out just waiting for you to finish with the code editor that popped up. You don’t need to worry about this, though. Once we add the necessary content to the code editor and finally close the code editor window, the Terminal will unfreeze and return to normal.
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
#
# Initial commit
#
# Changes to be committed:
# new file: css/app.css
# new file: index.html
# new file: js/app.js
#
The first paragraph is telling us exactly what we need to do — we need to supply a message for this commit. Also, any line that begins with the #
character will be ignored. Farther down it says that this will be the initial commit. Lastly, it's giving us a list of the files that will be committed.
Since this is the very first commit of the repository, we’ll use the commit message “Initial commit”. The text “Initial commit” isn’t special, but it’s the de facto commit message for the very first commit. If you want to use something else, feel free!
Type out your commit message on the first line of the code editor:
Finish Committing
Now save the file and close the editor window (closing just the pane/tab isn’t enough, you need to close the code editor window that the git commit
command opened).
Awesome, now switch back to the Terminal and you should see something like the following:
Bypass The Editor With The -m
Flag
TIP: If the commit message you’re writing is short and you don’t want to wait for your code editor to open up to type it out, you can pass your message directly on the command line with the -m
flag:
$ git commit -m "Initial commit"
This command:
- will open the code editor that is specified in your configuration
- (check out the Git configuration step from the first lesson to configure your editor)
Inside the code editor:
- a commit message must be supplied
- lines that start with a
#
are comments and will not be recorded - save the file after adding a commit message
- close the editor to make the commit
Then, use git log
to review the commit you just made!
Further Research
- Associating text editors with Git from GitHub Help Docs
- Getting Started — First-Time Git Setup from Git book
Good Commit Messages
How do I write a good commit message? And why should I care?
These are fantastic questions! I can’t stress enough how important it is to spend some time writing a good commit message.
Now, what makes a “good” commit message? That’s a great question and has been written about a number of times. Here are some important things to think about when crafting a good commit message:
Do
- do keep the message short (less than 60-ish characters)
- do explain what the commit does (not how or why!)
Do not
- do not explain why the changes are made (more on this below)
- do not explain how the changes are made (that’s what
git log -p
is for!) - do not use the word “and”
- if you have to use “and”, your commit message is probably doing too many changes — break the changes into separate commits
- e.g. “make the background color pink and increase the size of the sidebar”
The best way that I’ve found to come up with a commit message is to finish this phrase, “This commit will…”. However, you finish that phrase, use that as your commit message.
You can check it out on our Git Commit Message Style Guide.