The Git Commit Command: Tutorial & Examples

Updated: January 27, 2024 By: Guest Contributor Post a comment

Introduction

Git is a powerful tool for version control, allowing teams and individuals to track changes, collaborate and manage code in an organized way. One of the cornerstone commands in Git’s vast suite of functionality is the git commit command. A commit is a snapshot of your repository at a particular point in time. In this tutorial, we will explore the use of the git commit command in depth with examples ranging from basic to advanced usage.

Getting Started with Git Commits

Before making commits, you must have Git installed on your machine and have a repository to work with. If you’re new to Git, consider first learning the basic commands git init, git add, and git status.

Simple Commit

$ git add .
$ git commit -m "Initial commit"
[main (root-commit) 1e89037] Initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 index.html

This command will commit all currently staged changes to the repository with the message “Initial commit”.

Adding Files and Committing

$ git add README.md
$ git commit -m "Add README file"
[main 82e6915] Add README file
 1 file changed, 10 insertions(+)
 create mode 100644 README.md

It’s considered good practice to make small, logically separate commits so that the history is easier to follow.

Viewing Commit History

After the commit, you may want to view your commit history.

$ git log
commit 82e6915dbf48e9a7cd5754bb63c89e9ddfdda02b (HEAD -> main)
Author: Your Name <[email protected]>
Date:   Wed Mar 10 17:20:12 2023 +0100

    Add README file

The git log command will display a list of commits, including the commit hash, author, date, and message.

Understanding Commit IDs

Each commit has a unique ID, also known as a hash, that identifies it in the Git history. It is crucial when referring to specific commits.

Amending Commits

Amending allows you to correct the last commit.

$ git commit --amend -m "Corrected commit message"

Use this command with caution as it rewrites the commit history.

Using Commit Hashes

Commit hashes can be used to check out a particular state of your repository.

$ git checkout 82e6915

This will move the HEAD pointer to the commit with the given hash.

Advanced Committing

Committing Parts of Changes (Interactive Staging)

Start by adding the files interactively:

$ git add -p

Answer the prompts to determine what changes you wish to stage. Then commit those staged changes:

$ git commit -m "Refactor code for better readability"

Signing Your Commits

To show that a commit was made by you and hasn’t been tampered with, you can sign your commits with GPG.

$ git commit -S -m "Signed commit"

This requires a GPG key to be set up and associated with your Git configuration.

Squashing Commits

Squashing is combining multiple commits into one. This process will involve an interactive rebase:

$ git rebase -i HEAD~3

Follow the instructions Git provides in the interactive interface to squash the commits.

Stashing Changes Before Committing

If you’re not ready to commit but need to switch branches, you can stash your changes temporarily.

$ git stash
$ git checkout other-branch
// Do work on other-branch
$ git checkout original-branch
$ git stash pop

Once back on the original branch, you can apply your stashed changes and then commit.

Bypassing the Staging Area

For smaller changes, bypass the staging area and commit changes directly from the working directory with the -a option.

$ git commit -a -m "Update with inline commit"

This adds all the files that are already being tracked and commits the changes with the message provided.

Ignoring Files

Some files should not be committed to a repository. To ignore them, list them in a .gitignore file.

# .gitignore
node_modules/
.env

Any files or patterns listed in .gitignore will not be tracked by Git.

Conclusion

With a solid understanding of the git commit command, developers can ensure that their project’s history is clean, understandable, and serves as reliable documentation for changes made over time. Practice with these examples to refine your version control process and make your workflow more efficient.