Saving changes to a Git repository is not as simple as just saving a file. The Git version control system uses a more elaborate method of tracking changes, which allows developers to have more granular control over the changes they make to code in a repository.
The git commit command is one step in “saving” the changes made to a project to a repository. The git add command line function is first used to create a record of the changes that will be stored in a commit, then git commit is used to create a commit with those changes. Finally, git push is used to push the commit to a central repository.
In this tutorial, we’re going to explore, with examples, how to use the git commit command to create commits with Git. By the end of reading this tutorial, you’ll be an expert at creating commits using git commit.
Commits are an important part of the Git version control system.
One way to think about commits is that they are a snapshot of a project during a certain moment in the project’s history. When a commit is created, it creates a record of how all the files and directories appeared in a project at the time the commit was created.
This record can then be read in the future to see how a project looked at a point in time and who made what changes to what files in the project. Indeed, commits are an important feature of Git because it allows developers to keep a comprehensive record of how their repository has evolved over time.
Unlike in other version control systems, a commit does not affect a central repository until you are ready, which means that it can act as a gap between changes made to a repository and the main version of a repo.
In other words, instead of having to make a change directly to the main version of a repo—which may have many contributors actively watching the repo—a developer can commit their code in their local repo and push it to the main repository later.
81% of participants stated they felt more confident about their tech job prospects after attending a bootcamp. Get matched to a bootcamp today.
The average bootcamp grad spent less than six months in career transition, from starting a bootcamp to finding their first job.
Creating a commit is the second-from-last stage of “saving” a change in Git. After you create a commit, the next step is to use git push to push the changes you have made to the central repository.
How to Use Git Commit
The git commit command saves all the staged changes to the local repository.
“Staged changes” refers to all the files in the staging area, which are added using the git add command. To learn more about the git add command, you can read our beginner’s guide to git add.
Let’s explore how to use the git commit command.
The most basic usage of the git commit command is as follows:
This command commits the staged changes to the local repository. When this command is run, a text editor will open asking you to submit a commit message.
This message is where you should briefly describe the changes you have made to a repository in a commit. After you have written a commit message, you can close the text editor and then the commit will be created.
- Career Karma matches you with top tech bootcamps
- Get exclusive scholarships and prep courses
git commit -a
The -a flag, which stands for all, allows you to automatically stage all modified files to be committed.
Suppose you have a file called CONTRIBUTORS.md that you have committed to your repository. If you change this file, you can use the git commit -a command to stage and add the changes to your repository.
Using the -a flag allows you to skip over running “git add” on all changed files. This is because the -a flag instructs git to stage all modified files. However, the -a flag will not stage any new files. So, if you create a file called “README.md”, it will not be staged unless you run “git add” first.
Here’s the syntax for the -a flag:
git commit -a
This command will automatically stage all modified files to be committed in our repository.
git commit -m
Perhaps the most common flag used with git commit is the -m flag. The -m flag, which stands for message, is used to add a commit message to a commit.
When you use the git commit command without the -m flag, a text editor will be opened in which you can write a message, as we discussed earlier. However, using the -m flag allows you to skip over a text editor opening by directly specifying a commit message.
Here is the syntax for using the git commit command with the -m flag:
git commit -m "feat: commit message"
In this command, we specify the -m flag to indicate we want to add a commit message directly to our commit (instead of having a text editor open up where we add a message). Then, we enclose our commit message in quotation marks following the -m flag. In this case, our commit message is “feat: commit message”.
Git Commit Example
Let’s walk through an example to illustrate how you may use the git commit command.
Suppose we are working on a project and we have just edited the app.py file in our project. We have made all the changes we wanted to make to the file, and we are ready to create a commit for the file.
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
The first step is to add the file to the staging area using git add. You can do so using this code:
git add app.py
Now the app.py file is in the staging area. This means that we are able to commit it to our code when we are ready.
We have added a new function called “runApp()” in our app.py file, which we want to commit to the local repository. We can do so using this code:
git commit -m "feat: Add runApp() function"
This command creates a commit with the changes made to our app.py file. The message associated with our commit is “feat: Add runApp() function”, which briefly describes the changes we made to our repository.
Amend a Git Commit
The git commit command also allows for the previous commit to be amended. You can amend the previous commit using the –amend option.
Suppose we have just created our commit and we forgot to add the __init__.py file to our commit. We could amend our initial commit so that our file is included using this code:
git add __init__.py git commit --amend -m "feat: Add runApp() function and update init file"
This will amend our previous commit to include the __init__.py file, and will revise our commit to use the new commit message we specified in the above command.
The –amend flag is often used by software developers because it is easy to make mistakes when you’re versioning software. For instance, you may forget to stage a file to a commit, or you may make a typo when you are writing a commit message, or your commit message may not include a major change you made to your code.
Thus, you’ll likely use the –amend flag frequently when you are developing software.
Writing a Good Commit Message
As we have discussed, writing a commit message is mandatory when you are using the git command. While a commit message can technically include anything you want, you should spend some time thinking about what you want to include in your commit message.
While there are no rules on how you can write a good commit message, there are a few best practices that are often used by developers to ensure they write consistent and descriptive commit messages.
Here are a few things you should keep in mind when you are writing a git commit message:
- Capitalize the subject line of the commit message
- Limit the subject line to 50 characters
- Use the body of a commit to explain what you have changed (rather than why)
- Keep each line in the body of a commit to less than 72 characters.
Following these rules will allow you to write more readable commit messages which are easy to interpret.
In addition, you may want to refer to other issues or pull requests in your commit message. For instance, if a commit resolves issue #22 in a repository, you may title your commit “fix: resolves issue #22” and include a description in the body of the commit about what you changed to resolve the issue.
The git commit command is used to move files from the staging area to a commit. This command is run after git add, which is used to add files to the staging area. git commit creates a snapshot of the changes made to a Git repository which can then be pushed to the main repository when the developer is ready to do so.
This tutorial discussed how to use the git commit command to commit changes to a Git repository. Now you’re equipped with the knowledge you need to start using the git commit command like an expert!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.