Now that we have learned how to install git and start adding files to our staging index, we can now learn how to how to commit changes in Git.
Committing tracked files to the repository means that we are adding changes to the permanent record of the project. Imagine this process as similar to recording media to a cassette tape. New information is recorded, but you can play back your recording at any time, and you can also record over your previous recordings.
In Git, every recording is known as a “commit.” You are committing to changes you’ve made and recording them for future reference.
How to Make your Commit and Note It
To commit the changes you’ve made, you will only need to run a simple command in your terminal.
git commit
This command will launch your default text editor in order to get your “commit message.” The commit message is meant to be a brief statement regarding what this commit accomplishes. For example, if you added a new test feature, your commit message might look like this:
Added new test feature # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # On branch master # Changes to be committed: # modified: index.html # # Untracked files: # #index.org# # .#index.org #
You’ll notice the commit message at the top, highlighted in green. Below, commented out, you will see which file was modified in order to make this change. You will also see information about untracked files in the project. Once you save changes and close the text editor, the commit will be complete.
Skip the Editor
Often, it is faster to skip your text editor and make your commit message on the same command line as your commit itself. In order to do this, all you need to do is add the -m
option to the command:
git commit -m "Added new test feature"
This command will provide Git with a new commit along with the message “Added new test feature.”
Automatically Adding Changes
Remember from the previous article, about adding files to Git, that you must run the add command again in order to add file modifications to the staging index. Adding files only once does not mean that future changes will be staged or committed.
This means you will need to run the add command again in order to add new changes to the staging index.
git add .
Once staged, the modifications (and any new files that have just been added) can be committed to the repository.
However, it’s quite easy to skip the the add command and go straight to your commit. All you need to do is add the -am
option to the commit command:
git commit -am "Added new test feature"
The above command will stage and commit any changes made to the project. Now you know how to commit changes to the repository in Git. You are ready to learn about deploying files to your server.