Git Add and Status Explained

In the previous lesson, we learned how to create a file and add it to the staging area using git add.
Now we’ll explore the different ways of using git add, and how git status (including its short version) helps us keep track of what’s happening in our repository.


Checking the Staging Area

When you run git status in a new repository, this is what you see:

git status

Output:

gaurav@learning-ocean:~/git-tutorials$ git status
On branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)

This tells us that no files are tracked yet. Let’s create a new file:

echo "first file in git repo" > first.txt

Now check again:

git status

Output:

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        first.txt

👉 first.txt is untracked. Git knows it exists but isn’t tracking it yet.


Adding Files to the Staging Area

To start tracking, we use:

git add first.txt

Now check status:

gaurav@learning-ocean:~/git-tutorials$ git status
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   first.txt

🎉 Our file is now staged and ready for commit.


Short Status View

Sometimes, the full git status output is too verbose. Instead, use:

git status -s

Output:

A  first.txt
  • A means the file is added to the staging area.
  • ?? means a file is untracked.
  • M means a file is modified.
  • AM means the file was added earlier, but has been modified again.

Adding Multiple Files

Let’s create a few more files:

echo "this is second file" > second.txt
echo "this is third file" > third.txt

Check status:

gaurav@learning-ocean:~/git-tutorials$ git status
Changes to be committed:
        new file:   first.txt

Untracked files:
        second.txt
        third.txt

Add the second file:

git add second.txt

Now status:

Changes to be committed:
        new file:   first.txt
        new file:   second.txt

Untracked files:
        third.txt

And in short mode:

git status -s
A  first.txt
A  second.txt
?? third.txt

Modifying a File After Adding

What if we modify first.txt after staging it?

echo "first modified again" > first.txt

Now:

git status -s
AM first.txt
A  second.txt
?? third.txt
  • AM first.txt → The file is staged (A) but modified again (M). This tells us the staging area has an older version, while the working directory has new changes.

To bring the new changes into staging:

git add first.txt

Adding All Files

Instead of adding files one by one, you can add everything at once:

git add .

Status:

Changes to be committed:
        new file:   first.txt
        new file:   second.txt
        new file:   third.txt

🎉 All files are staged together.


Adding Only Certain File Types with Patterns or Regex

Sometimes you only want to add files of a specific type. For example, let’s create a few files:

touch a.out output.pyc program.py program1.py

Now suppose we only want to stage Python files. We can use:

git add *.py

This will add all files ending with .py (like program.py and program1.py) to the staging area.

You can also use Git’s support for regular expressions for even more control. For example:

git add -e '.*\.py$'

Explanation:

  • .* → match any file name
  • \.py → match .py extension
  • $ → match the end of the filename

This ensures only files ending in .py are staged.

💡 Tip: Always wrap the regex in quotes to avoid issues with shell expansion.

This pattern-based approach is super helpful when you have many mixed file types and need to add only specific ones, like .html, .css, or files starting with a particular prefix.


Summary

In this blog, we covered:

  • How to use git status and git status -s
  • Meaning of symbols (??, A, M, AM)
  • How to add files one by one
  • How to add multiple files
  • How to add everything with git add .
  • How to add only specific files using patterns and regex

🚀 Don’t Miss Out!

If you enjoyed this blog and want to learn Git step by step with me, then you’ll love what’s coming next.

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

👉 My YouTube Channel – More videos, more fun, and lots of learning!

👉 📺 Watch this topic in video form

Stay curious, keep coding, and let’s make learning fun together! 🎉