Recording changes
At this stage, the repository doesn't contain any files or changes. Yes, we have the file hello.html
in the work tree, but it's not yet under version control. We need to explicitly add any changes to the repository.
A change in the Git repository is called a commit. You can think of a commit as a snapshot of the repository at a certain point in time. It contains all the changes that were made since the last commit. You can also add a message to the commit to describe what changes were made.
Before creating a commit, we need to tell Git what changes we want to put into that commit. In Git terms, we need to add all the relevant changes to the staging area. It's a fancy way of selecting files that we want to add to the next commit.
Now let's add our "Hello, World" page to version control. To do that, we will use the git add command. This command has a parameter that specifies the file we want to add to the staging area. In our case, it's hello.html
.
git add hello.html
Add the file hello.html
to the staging area of the repository.
Now, we're about to make our first commit. We can do that using the git commit command like this:
git commit -m "Initial commit"
This command has an important option -m
(shorthand for --message
) that allows us to specify the commit message. We will use the -m
option to provide a short description of the changes we made in this commit. It's a good practice to write meaningful commit messages that describe the changes you made. This will help you and your team understand what changes were made in the future. If you don't specify the -m
option, a default text editor will be opened, where Git expects you to write the commit message.
If you did run the commit command without the -m
option, you may have noticed that your terminal changed: the history is gone and you are presented with a blank screen. This is a text editor that Git has opened for you to write the commit message. You can write the message in this editor, save it, and close it to complete the commit. The editor is usually a command line text editor like vim
or nano
.
Sounds easy, what could go wrong? People not familiar with these editors may find it difficult to even exit them. If you're one of them, here are some videos that will help you to exit these editors: Vim, Nano. If nothing helps, kill the terminal with the little icon on the right side of the terminal and start over.
In the example above, the "Initial commit"
piece is the value of the -m
option, our commit message. Note that the commit message should be in quotes so that the whole message would be treated as a single value. It will be recorded without the wrapping quotes.
If you must have a quotation mark as part of the message, you can place a backslash (\
) character before that quotation mark to escape it. In context of the command line and text strings overall, escaping usually means stripping something of special powers and treating it as plain text; here the special power is closing the string. Alternatively, you can wrap the commit message with single quotation marks. For example:
git commit -m "This is a \"message\""
OR
git commit -m 'This is a "message"'
Commit the file hello.html
to the repository.
You should see something like this as output:
[main (root-commit) 571a4dd] Initial commit
1 file changed, 1 insertion(+)
create mode 100644 hello.html
It's a good practice to divide independent changes into separate commits. For example, if you added a new feature and fixed a bug, it's better to make two separate commits instead of one. This way, you can easily revert the new feature without losing the bug fix. This practice is called atomic commits.
Sometimes the staging area of the repository is also called the index. If someone says index this file, they actually mean add the file to the staging area of the repository.
Hi! I'm Alex, creator of GitByBit.
This page is a part of the interactive course about Git version control.
It's a one-of-a-kind course that is integrated into the VS Code code editor. Learning directly in VS Code lets you operate Git exactly as you would in real life, doing real jobs, writing real code.
In addition, the course has access to your actual terminal, so it can point out mistakes, suggest workarounds, etc.
The course is FREE, there are no Ads or other bullshit. There are optional premium add-ons you can purchase, mainly to support my work (regrettably, I have to eat every day), but that's totally up to you.