0
0
GitHow-ToBeginner · 3 min read

How to Commit in Git: Simple Steps to Save Your Changes

To commit in Git, use the git commit -m "your message" command after staging your changes with git add. This saves a snapshot of your project with a message describing the changes.
📐

Syntax

The basic syntax to commit changes in Git is:

  • git commit: Creates a new commit with staged changes.
  • -m "message": Adds a short message describing the commit.

You must stage files first using git add before committing.

bash
git add <file>
git commit -m "Your descriptive commit message"
💻

Example

This example shows how to stage a file and commit it with a message.

bash
echo "Hello Git" > example.txt
git add example.txt
git commit -m "Add example.txt with greeting"
Output
[main abc1234] Add example.txt with greeting 1 file changed, 1 insertion(+) create mode 100644 example.txt
⚠️

Common Pitfalls

Common mistakes include:

  • Trying to commit without staging files first.
  • Using unclear or empty commit messages.
  • Forgetting to save changes before adding.

Always stage your changes and write clear messages.

bash
git commit -m "Forgot to add"
# This will fail if no files are staged

git add file.txt
git commit -m "Add file.txt with updates"
# Correct way
Output
On branch main nothing to commit, working tree clean [main abc1234] Add file.txt with updates 1 file changed, 2 insertions(+)
📊

Quick Reference

CommandDescription
git add Stage changes to be committed
git commit -m "message"Commit staged changes with a message
git statusCheck current changes and staged files
git logView commit history

Key Takeaways

Always stage your changes with git add before committing.
Use git commit -m "message" to save changes with a clear message.
Check your changes with git status before committing.
Write meaningful commit messages to describe your changes.
Avoid committing without staging files first to prevent errors.