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.
Table of Content
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.
| Action | Git Add | Git Commit |
|---|---|---|
| Purpose | Stages changes | Records staged changes |
| Scope | File or folder level | Repository snapshot |
| Usage | Before commit | After 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~1This 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?
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?
-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?
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
If you're diving into coding, your introduction to Git and GitHub will happen in no time. Git's your personal time…
You can use Git to show all branches or use branch-related commands to list branches and their status. This helps…
When you work on a project with friends, one writes the intro, one adds pictures, and others handle different parts.…
Git Pull Rebase works to keep your commit history simple. Git Pull Rebase helps you reduce merge noise in your…
You can use the Git config command to set user data and control Git behavior. It saves preferences for projects…
The switch operation in Git will make it easier. Designed to simplify the process for developers, it's safer and more…
A Git branch is a lightweight movable pointer to one of your commits. A feature or fix is thought of…
You sometimes need to remove old or unneeded branches locally in Git. You might want to keep your project clean…
If you've worked on any coding project, you would know this frustrating wall where Git simply refuses to pull the…
When you work with teams, you often need to remove old remote branches. To delete a remote branch in Git,…