0
0
GitHow-ToBeginner · 3 min read

How to Use git commit -am: Quick Guide and Examples

Use git commit -am "message" to stage all modified and deleted files and commit them with a message in one step. This command skips new untracked files and only works for files already tracked by Git.
📐

Syntax

The git commit -am command combines two actions: -a stages all modified and deleted tracked files, and -m adds a commit message directly. It does not stage new untracked files.

  • -a: Automatically stage tracked files that have changes.
  • -m "message": Provide the commit message inline.
bash
git commit -am "Your commit message here"
💻

Example

This example shows how to modify a tracked file, then use git commit -am to stage and commit the change in one step.

bash
echo "Hello World" > file.txt
# Assume file.txt is already tracked by git

# Modify the file
echo "More text" >> file.txt

# Commit changes with message
git commit -am "Add more text to file.txt"
Output
[main abc1234] Add more text to file.txt 1 file changed, 1 insertion(+)
⚠️

Common Pitfalls

1. git commit -am does not stage new files. You must add new files first with git add.

2. It only stages modified and deleted tracked files, so untracked files are ignored.

3. Forgetting to provide a commit message with -m will cause an error.

bash
git commit -am "Fix bug"
# If you have new files, they won't be included

# Correct way to add new files:
git add newfile.txt
git commit -m "Add newfile.txt"
📊

Quick Reference

OptionDescription
-aStage all modified and deleted tracked files automatically
-m "message"Provide commit message inline
Does not stage new untracked filesUse git add for new files before committing

Key Takeaways

Use git commit -am "message" to stage and commit tracked file changes in one step.
New files must be added with git add before committing; -am does not include them.
Always provide a commit message with -m to avoid errors.
The -a flag only stages modified and deleted tracked files, not untracked files.