The Complete Beginner’s Guide to the Git Commit Command

Git is the most popular version-control system system in the world. It’s a big part of the development process, so it’s good to understand the basics, starting with what is a git commit command?

git commit is a common command in git. It creates a commit, which is basically a snapshot of a certain time in your repository.

In this article we’ll do a quick overview of git before diving deeply into git commit. We’ll go over what git commit does, what a commit should contain, how to undo it, and more.

Feel free to jump ahead to one of the sections below by clicking on the section title:

  1. What is git?
  2. What is a git commit command?
  3. What does a git commit contain?
  4. When should I use git commit command?
  5. What happens after git commit?
  6. What’s the difference between git commit and git push?
  7. Can I undo a git commit?
  8. Final thoughts

1. What is git?

We mentioned git is a version control system, but what does that mean? Git allows developers to track changes to the files in their code repository. If you accidentally introduce a bug to the codebase, you’re able to go back and look at the previous version of the code.

At most companies you have many developers working within the same codebase. Git helps teams better collaborate, by allowing the creation of branches, i.e. different versions of the repository. This prevents everyone from overwriting each other’s code. This makes it easier for developers to work on different features at the same time.

Once a feature is done, a developer can open up a pull request. This is a request to merge their code in with the rest of the codebase.

All this time git is working in the background recording the changes in a special database called a repository. Thanks to git you can keep track of who has made what changes, when, and why.

If you’d like to learn more about it, as well as how to differentiate it from the similarly-named GitHub, check out our beginner’s guide to Git vs GitHub.

2. What is a git commit command?

One of the major commands you will use when working with git is git commit.

A commit is a snapshot of your entire repository at a specific time. When you create a commit, it makes a record of exactly how all the files and directories looked at the moment of creation.

git commit has two phases. First you use git add to add the files you want to appear in your commit. Before adding those, you can run git status to check which files have changes and look through them. To add all the changes, you can run git add . (with a dot at the end). 

Once you’re ready to commit, run git commit -m "your descriptive commit message" explaining what was completed in that commit.

3. What does a git commit contain?

An example of a git commit message is:

git commit -m "Improve performance with lazy load implementation for images"

Besides your message and the content of your code, a commit also stores various metadata like the author of the commit and the timestamp.

The commit message is required and important for painting a clear history of what was done in the repository over time. There are several best practices concerning writing a good commit message.

  • Capitalize the first letter of the commit, i.e. "Fix bug with global search input deleting user text"
  • Keep it short. Try to keep your message under 50 characters
  • Focus on what you have changed, rather than why you changed it

4. When should I use git commit command?

Ideally as you’re writing code, you commit at each logical stopping point. This not only saves your work as you complete subtasks, but creates a history of your repository. Consider commits as “save points” in your work.

Usually you want your commit to focus around a specific idea. What if you got a bit carried away, fixing or dealing with multiple topics at once? You can add files one at a time into commits by using git add /YOUR-FILE-PATH for each file. This way you can break up your changes into multiple separate commits, if needed.

5. What happens after git commit?

Once you run git commit, the files are moved from the staging area to your commit. Commits are created on the branch you’re currently “checked out” on.

Pro tip: Make sure you’re on the right branch before committing to save headaches later!

The staging area is a buffer between what you’re currently working on and the main repository. Although, depending on your project structure, it could be the area between your work and the development branch, the feature branch, etc.

6. What is the difference between git commit and git push?

Creating a commit is the step right before saving a change to git. git push is the command that actually saves the changes to the repository. The main difference is that after you make a commit, your changes are still in your local repository. Once you run git push, the changes are pushed from your local repository into the remote repository.

Essentially, git push transfers your changes to the (usually shared) remote repository. There they become part of the larger picture of your code base and other people can see what you’ve done.

An example of a git push is git push origin master or git push origin dev (if going to the development branch).

7. Can I undo a git commit?

Let’s say you committed something to the wrong branch. What to do now?

There are a few ways to tackle this.

First, consider if the commit you want to undo is on a branch being shared with collaborators. If a shared branch, you’ll likely want to choose a method that doesn’t totally delete the history.

This is where git revert comes in.

What is git revert?

git revert is the safest way to undo a commit (and adjust your project’s history) with git. Instead of permanently deleting the changes from the last commit, it keeps the record of those changes and undoes them. When it finds the changes, it adds the inverse of those changes to a new commit.

What is git reset?

git reset will actually delete the changes from the history of your project. Beware of this, as it’s a very powerful command. git reset can lead to permanently losing your work (or that of your colleagues).

Still, there are some cases where this is necessary. For example, when sensitive information like a token or password was included in a commit. In this case, you don’t want anyone to be able to find that in your git history. If using this on a shared remote branch, make sure to discuss it with your collaborators.

Final thoughts

As we can see, the git commit command is common in the git flow to move files from the staging area (added with git add) to a commit. A commit is a picture of your repository at a moment in time. Commits help craft the history of your repository. 

Ideally commits are created at logical change points as you write your code. They should also have short but descriptive messaging explaining what was changed. Once ready, you can push your commits to the remote repository (master, dev, your feature branch, etc.). This is when your code will join the main repository.

As you can see here, Git is a big part of the development flow. As a result, it’s good to familiarize yourself with git commit and its related commands: git add, git status, git push, git log, etc.

If you’d like to read more about the world of web development, check out these articles:

What You Should Do Now

  1. Get a hands-on introduction to web development and build your first website from scratch with our free, self-paced Web Development Short Course.

  2. Take part in one of our FREE live online web development events with industry experts.

  3. Talk to a program advisor to discuss career change and how you can become a qualified web developer in 4-7 months—complete with a job guarantee.

  4. Apply for one of 100 Career Change Scholarships before the end of June and get up to $1,295 off the CareerFoundry Full-Stack Web Development Program.

What is CareerFoundry?

CareerFoundry is an online school for people looking to switch to a rewarding career in tech. Select a program, get paired with an expert mentor and tutor, and become a job-ready designer, developer, or analyst from scratch, or your money back.

Learn more about our programs