Git Commit Command Explained with Examples

In Git, a commit is like taking a snapshot of your project at a specific point in time. Imagine writing code today, then adding more after two days. Each time you want to capture the state of your project, you make a commit. Later, even after six months or a year, you can “time travel” back to see how your code looked at that moment.

This blog will walk you step by step through the git commit command and how it saves your work in the Git repository.


What is a Commit in Git?

  • A commit is a saved snapshot of your project files.
  • Git commits only what is in the staging area, not your entire working directory.
  • Every commit has a message describing the change.

Think of commits as milestones in your project journey.


How Git Commits Work (Visual)



Checking the File and Status

Suppose we have a file first.txt inside a Git repository.

cat first.txt

Output:

This is the first file
With two lines of text

Now check the status:

git status

This shows which files are staged and which are still in the working directory.


Making the First Commit

To commit your changes, run:

git commit -m "First commit"
  • -m lets you write a short, meaningful commit message.

  • Example messages:

    • user module created
    • login feature completed and tested

Always use descriptive commit messages when working professionally.

After committing, check the status:

git status

Output will show:

nothing to commit, working tree clean

Modifying Files and Committing Again

Let’s update first.txt:

echo "New line added in first.txt" >> first.txt

Now check status:

git status

Output shows that first.txt is modified.

Stage the file:

git add first.txt

Now commit again:

git commit -m "Updated first.txt with a new line"

Adding Another File

Suppose you also modify second.txt:

echo "Second file content updated" >> second.txt

Check the status:

git status

Both files show as modified. But if you stage only first.txt:

git add first.txt
git commit -m "Committed first.txt only"
  • Only first.txt is committed.
  • second.txt remains modified in the working directory.

To commit second.txt, stage and commit it separately:

git add second.txt
git commit -m "Updated second.txt"

Now your working tree is clean again.


Wrap-up

The git commit command is the backbone of version control. It lets you record snapshots of your project, making it possible to revisit any point in history later.

In the next blog, we’ll learn how to see the history of commits and navigate through them.


Keep Learning 🚀

👉 Subscribe to Learning Ocean – Subscribers get coupon codes, early access to blogs/courses, and exclusive YouTube videos.

👉 My YouTube Channel

👉 Watch this video explanation

Stay curious, keep coding, and let’s master Git together! 🎉