Git Commit -a Explained and Best Practices

In the last blog, we saw how to amend commits and view commit history with git log.
In this blog, let’s take a closer look at the git commit -a command, how it works, and why you should use it carefully.


What is git commit -a?

Normally, committing requires two steps:

  1. Stage changes with git add.
  2. Commit with git commit -m "message".

With git commit -a, Git automatically stages all tracked files (modified or deleted) and then commits them in one step.

⚠️ Important:

  • It does not include untracked files.
  • New files must still be staged manually before committing.

Example: Modifying and Deleting Files

Let’s modify two files and delete one:

vim first.txt
vim five.txt
rm temp.py

Check the status:

git status -s

Output:

M first.txt
M five.txt
D temp.py

Now commit everything in one go:

git commit -a -m "Commit without staging"

Output:

[main abc123] Commit without staging
 2 files changed, 1 deletion

Check history:

git log --oneline

Output:

abc123 Commit without staging
def456 Previous commit

Example: Adding a New File

Let’s modify an existing file and add a new one:

echo "extra line" >> first.txt
echo "new file content" > newfile.txt

Check the status:

git status -s

Output:

M first.txt
?? newfile.txt

Now run:

git commit -a -m "Auto commit with -a"

Result:

  • first.txt is committed (because it is tracked).
  • newfile.txt is ignored (because it is untracked).

You must explicitly stage it:

git add newfile.txt
git commit -m "Added new file"

Best Practices for Committing

  • Avoid committing too frequently for every small change.

  • Make commits meaningful, such as:

    • “User module completed”
    • “Login feature implemented and tested”
  • Keep your commit history clean so that debugging and code review become easier.

  • Use commits to mark features or ticket completions, not single-line changes.


How Git Commit -a Works (Visual)



Summary

In this blog, we learned:

  • How git commit -a can save time by skipping the manual staging step.
  • Why it only works on already tracked files.
  • Why relying too much on this command can lead to mistakes.
  • The importance of writing meaningful commits and keeping history clean.

In the next blog, we’ll dive deeper into Git Logs and explore how to use them effectively.


Keep Learning 🚀

👉 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! 🎉