Git commit Command: How it Works with Examples

git commit command

The commit command in Git records changes in a local repository. It stores a snapshot of files. It also adds a message to describe the changes. You can then push commits to a remote repository.

Understand the git commit Command

The git commit command saves staged changes to the local repository. It takes the content from git add and writes a new snapshot. It also stores a message about the change.

Here is the command:

git commit -m "message"

This command records staged changes to the repository with a message. The message helps identify the purpose of the commit.

You can use -m to add a message or use -a to commit tracked files directly. You can also use --amend to change the last commit.

Git commit takes a snapshot of the project. It also links this snapshot with the previous one. This creates a history of changes. You can view the commits with git log.

To undo or change a commit, use git reset HEAD~1 to undo the last commit and keep the changes in the working area. Use git commit --amend to change the message or add files to the last commit.

The Difference Between Git Add and Git Commit

Git add puts files into the staging area. Git commit takes files from the staging area and stores them in the local repository.

ActionGit AddGit Commit
PurposeStages changesRecords staged changes
ScopeFile or folder levelRepository snapshot
UsageBefore commitAfter add

You use git add to mark files and then use git commit to store the changes.

Examples

Basic Commit with a Message:

git add file.txt  
git commit -m "Add file.txt"

This example stages file.txt and then records it with a message. It shows the simplest way to save changes.

Commit All Tracked Files:

git commit -a -m "Update tracked files"

This example commits all tracked files without running git add. It saves time when you update many files.

Amend the Last Commit:

git commit --amend -m "Update last commit message"

This example changes the message of the last commit. It can also include new files that you forgot to add before.

Undo the Last Commit but Keep Changes:

git reset HEAD~1

This example removes the last commit but leaves changes in the working area. It helps you fix mistakes without losing work.

Wrapping Up

You learned what git commit does and how to use it.

Here is a quick recap:

  • git commit records staged changes with a message, git add prepares files for commit, and you can amend or undo commits when needed.

FAQs

What is git commit command in Git?

Definition: The git commit command records changes to the repository. Purpose: It creates a snapshot of staged changes and saves history.
  • Used after git add
  • Creates a new commit object
  • Includes message and metadata

git commit -m "Your commit message"

How do you use git commit command with a message?

Syntax: Use the -m option to add a commit message.

git commit -m "Add new feature"
Notes:
  • Always write clear messages
  • Message should explain change
  • Avoid vague text like "update"

What is the difference between git add and git commit?

  • git add: Stages changes for commit
  • git commit: Saves staged changes permanently

git add file.txt  
git commit -m "Commit staged file"

How to undo or amend a git commit?

Amend last commit:

git commit --amend -m "Updated commit message"
Undo last commit (keep changes):

git reset --soft HEAD~1
Undo last commit (remove changes):

git reset --hard HEAD~1

Similar Reads

Git Introduction: Git and GitHub Explained Guide

If you're diving into coding, your introduction to Git and GitHub will happen in no time. Git's your personal time…

Git List Branches: How to Show All Local & Remote Branches

You can use Git to show all branches or use branch-related commands to list branches and their status. This helps…

Git Pull: How to Keep Your Code in Sync

When you work on a project with friends, one writes the intro, one adds pictures, and others handle different parts.…

Git Pull Rebase: How It Works, and When to Use It

Git Pull Rebase works to keep your commit history simple. Git Pull Rebase helps you reduce merge noise in your…

Git config Command: A Complete Tutorial to Configure Git Settings

You can use the Git config command to set user data and control Git behavior. It saves preferences for projects…

How to Switch Branches in Git: git switch vs checkout

The switch operation in Git will make it easier. Designed to simplify the process for developers, it's safer and more…

Git Create Branch: Simple Steps in 50 Seconds

A Git branch is a lightweight movable pointer to one of your commits. A feature or fix is thought of…

How to Delete a Branch Locally in Git

You sometimes need to remove old or unneeded branches locally in Git. You might want to keep your project clean…

Git Pull Force: Safely Overwrite Local Changes

If you've worked on any coding project, you would know this frustrating wall where Git simply refuses to pull the…

How to Delete a Remote Branch in Git

When you work with teams, you often need to remove old remote branches. To delete a remote branch in Git,…

Previous Article

JavaScript toSorted Function Guide with Examples

Next Article

PHP array_multisort Function with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.