2) Standard Git Workflow Lesson

Git Commit

15 min to complete · By CodingNomads Team

Afraid to commit? You're not the only one. In this lesson, you'll practice employing git commit as a part of your standard Git workflow so you can track changes and collaborate without fear. You'll work through the following examples and concepts:

  • What is Git Commit?
  • Git Commit Example
  • Git Commit Message
  • Git: Change Commit Message
  • Git Commit: -a
  • Git Help
  • Git Log

What is Git Commit?

The git commit command is used to add all changes in the staging area to the local Git repository.

As you learned previously, the git add command "adds" changed files and new files that are currently in the working directory to Git's staging area. The git commit command accomplishes the next and final step in the standard Git workflow: It "commits" everything in the staging area under version control and into the local Git repository.

Git Workflow

Why Commit? A common question at this point is:

Why does it take two steps to commit a file? Why do I need to add and commit?

This is a great question that actually has a relatively straightforward answer: It allows you to record edits across multiple files in one coherent logical unit of change.

Git Commit Example

Imagine that you are collaborating on a project with a team of developers. Everyone needs to be able to easily understand what is going on in the repository, even if they didn't make the changes themselves.

Illustration of a lighthouse

DANGER ALERT! A customer reported a bug related to Email Alerts. The team assigns you the responsibility to find and fix the bug. You are an insightful debugger and capable developer, so you manage to fix the error and increase the stability of the project's code base. To fix the bug, you needed to modify three different project files. However, all changes are necessary for the problem to go away, so committing each file by itself would make it much more difficult to understand what you did and why.

Because of Git's two-step process, you are able to add all three modified files to your staging area and commit them together with a descriptive commit message such as, "Fix: Email Alerts Bug". You are able to commit code as a logical unit. This makes it easier to understand changes done to the code base for yourself and others across the team. It also enables you and your team to revise changes in case something goes wrong.

Git Commit Message

When you commit changes to the Git repository, you are required to add a commit message. The Git commit message briefly describes the changes you're committing. Later in the course, you'll learn more about some best practices when writing commit messages.

The easiest way to execute a commit and add a message while doing so is to add the -m flag to the git commit command, followed by the message.

For this exercise, you'll use the temp_testing_repo you previously created in GitHub and cloned to your machine. Open a terminal and cd into the base working directory of the temp_testing_repo. This folder should contain the NewFile1.txt and .git directory from before.

You previously created the NewFile1.txt and added it to the staging area using git add. If you run git status now, you'll see that you have one new file that is staged for commit:

Git Status File Added Git Status Tracked File

It's finally time to put this file under Git version control. To do this, run the git commit command. This will permanently add the current state of the file into the Git repository's history.

Remember that you also need to add a commit message. Add it by using the -m flag, as shown below:

$ git commit -m "initial commit - NewFile1.txt"

You will see the following output:

[main 5035de5] initial commit - NewFile1.txt
1 file changed, 1 insertion(+)
create mode 100644 NewFile1.txt
Colorful illustration of a light bulb

Note: If you do not use the -m flag, your CLI will open up your default CLI text editor, which is usually vim, and you will need to add your commit message there, then save the file and quit vim.

Well done, your commit was successful! If you run git status now, you'll see that your "branch is ahead of 'origin/main' by 1 commit":

$ git status

On branch main
Your branch is ahead of 'origin/main' by 1 commit.
 (use "git push" to publish your local commits)

nothing to commit, working tree clean

In this message, origin is a reference to the remote repo on GitHub, and main is a reference to the branch on GitHub. This will make more sense soon. For now, know that Git is telling you with this message that your local Git repository has changes in its history that the remote repository doesn't have.

This wraps up the most common use of git commit, and with it, the basic Git workflow. Before moving on to learn more about collaboration with Git, here are a few tips on how to increase your productivity when committing files to your repository.

Git: Change Commit Message

If you added a non-descriptive commit message in haste or desperation, but your next-day-sane-self is eager to make amends, you can use the git amend commit message command:

$ git commit --amend

The $ git commit --amend command allows you to change your Git commit message. It will bring up your default text editor so you can amend the commit message. Once you save and quit, you're good to go!

Git Commit: -a

Don't commit too fast! Although sometimes there might be a reason for doing so. Just the same, handle the following options with care:

  • git commit -a
    This command commits all the changes made in the working directory. It skips the git add step. It only includes modifications to files that have been added with git add at some point in their history, also called tracked files. The command is a mix of git add and git commit
  • git commit -am "Commit message"
    This command is a power-user shortcut command that combines the -a and -m options. The combination immediately creates a commit of all the staged changes and allows you to pass an inline commit message.

Git Help

Git's got you! You can always use --help to see the full manual of a Git command, for example, with git commit:

$ git commit --help

You can also run git add --help, git clone --help, etc., for information on any other Git command. The --help argument will print out the documentation for the given command. This includes its option flags as well as the descriptions of said options:

Output of Git Commit Help

If you're just tuning in or simply want some review, check out the previous lesson on how to use Git Help.

Git Log

The git log command will show you the history of Git commits for your current project. You'll learn about this command in more detail in a later section.

Colorful illustration of a light bulb

Info: If you want to skip ahead and read more about the git commit command, feel free to review the Git documentation.

Summary: Git Commit

The git commit command is used to add all changes in the staging area to the local Git repository. In this lesson, you practiced employing git commit as a part of your standard Git workflow. You worked through the following:

Git Commit Example Walking through an example, you learned that because of Git's two-step process, you are able to add modified files to your staging area and commit them together with a descriptive commit message, allowing you to commit code as a logical unit. Git commits make it easier to understand changes done to the code base for yourself and others across the team while also enabling you to revise changes in case something goes wrong.

Git Commit Message The Git commit message briefly describes the changes you're committing.

Git Commit -a Running git commit -a commits all the changes made in the working directory, skipping the git add step. It only includes modifications to files that have been added with git add at some point in their history. It's a mix of git add and git commit. Handle with care!

Git: Change the Commit Message Use git commit --amend to change the commit message.

Git Help Don't forget: Git's got you! You can always use --help to see the full manual of any Git command, including git commit --help.

Git Log The git log command shows the history of Git commits for your current project.

With the basic Git workflow in your pocket, it's now time to move on to learn about what it takes to use Git and GitHub as collaboration tools. But first, a video about git commit!