GIT - 4.8 Express Commits

This lesson shows how to edit a file that is already tracked by Git, and how to commit changes faster using the -am shortcut. The starting point is a clean repository containing two tracked files: coucou.html and venus.html. We open and modify the cuckoo file:

code coucou.html

After adding some text, saving, and closing the editor, git status reports that coucou.html is modified. Important nuance: in earlier lessons the file appeared as untracked, and the changes had to be added before being committable. Now the file is tracked, so Git instead shows it as modified — changes not staged for commit. That is how Git distinguishes between brand new files and edits to existing ones.

Inspecting which files Git actually tracks

To know which files Git is currently following, we use:

git ls-files

Git answers with the list of tracked files: coucou.html and venus.html. To confirm the rule, we briefly create a brand new file touch terre.html and run git ls-files again — it is not listed, because Git is not tracking it yet. We delete the temporary file with rm terre.html to keep the example clean.

The express commit: git commit -am

This setup matters because the next command works only on tracked files. The -a flag on git commit tells Git to first auto-stage all modified tracked files, then commit them — combining two steps into one. Combined with -m for the message in the same parameter, it becomes:

git commit -am "update"

The commit is finished in a single line. Running git log now lists two commits, with the most recent at the top. Important to remember: commit -am only stages files already tracked — brand new untracked files still need an explicit git add first.

Summary

This lesson demonstrates how to efficiently commit changes in Git using the express commit workflow. The lesson covers the distinction between tracked and untracked files, how to use `git status` to monitor changes, and most importantly, how to leverage the `-a` flag with `git commit -a` or combine it with `-m` using `git commit -am` to stage and commit all modified tracked files in a single command, bypassing the separate staging step for faster workflows.

Key points

  • Tracked files vs untracked files: Use `git ls-files` to verify which files Git is monitoring
  • The `-a` flag stages all tracked, modified files automatically when committing, eliminating the need for a separate `git add` step
  • The `git commit -am` command combines both staging and committing in one action, providing the fastest commit workflow for modified tracked files
  • New untracked files still require `git add` before committing, as the `-a` flag only affects already-tracked files
  • Use `git log` to verify your commits were created with the correct messages

FAQ

What is the difference between `git commit -a` and `git commit`?

`git commit -a` automatically stages all changes to tracked files and commits them in one command. Regular `git commit` without `-a` only commits files that have already been staged with `git add`. The `-a` flag saves time by skipping the explicit staging step for modified tracked files.

Does `git commit -am` work on untracked (new) files?

No. The `-a` flag only stages modifications to files that are already tracked by Git. New untracked files must first be added with `git add` before they can be committed, even when using `git commit -am`.

How can I verify which files are tracked before using `git commit -a`?

Run `git ls-files` to see all files currently tracked by Git. You can also use `git status` to see both tracked files with modifications and untracked files. Only tracked, modified files will be staged and committed with the `-a` flag.