Tracking Changes: git status, git add, git commit
Learn how to stage and commit changes effectively.
Git is a powerful version control system that allows developers to track and manage changes in their projects with precision and efficiency. One of the most fundamental workflows in Git involves three essential commands: git status
, git add
, and git commit
. Mastering these commands is crucial for maintaining a clean, understandable project history and ensuring collaborative work runs smoothly. In this post, we'll unravel how these commands work together to help you stage and commit your changes effectively.
Understanding the Git Workflow
Before diving into the specifics of each command, it's important to grasp Git's basic workflow. When you work on a project tracked by Git, your changes progress through three main states:
- Modified (Unstaged): Files have been changed but not yet marked for inclusion in the next commit.
- Staged: Files are marked and ready to be committed.
- Committed: Changes are safely stored in the repository history.
The trio of git status
, git add
, and git commit
helps you navigate these stages.
git status: See the State of Your Project
The git status
command is your window into what is happening in your repository. It provides information about which files have been modified, which are staged for commit, and which are untracked.
Example
$ git status
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
modified: app.js
modified: styles.css
Untracked files:
(use "git add <file>..." to include in what will be committed)
new-script.js
What does this mean?
- Modified, not staged: You made changes to
app.js
andstyles.css
, but these changes are not yet ready for commit. - Untracked files:
new-script.js
is a new file Git is not yet tracking.
Key usage
Run git status
often. It helps you avoid surprises by showing you what's changed, what's staged, and what's new.
git add: Prepare Files for Commit
Once you've reviewed your changes with git status
, use git add
to move them from the "modified" state to the "staged" state. Only staged changes are included in your next commit. This allows you to precisely choose which updates you want to capture in your project history.
Adding a Single File
$ git add app.js
Adding Multiple Files
$ git add styles.css new-script.js
Adding All Changes
$ git add .
git add .
stages all modified and untracked files in the current directory and subdirectories.
Practical advice
Be intentional with staging. Use git add <file>
for granular control, especially when working on multiple features or bug fixes simultaneously.
git commit: Save Your Changes
After staging your files, use git commit
to permanently record the changes in your local repository. A commit acts as a snapshot of your project at a particular point in time.
Basic Commit
$ git commit -m "Fix header alignment on home page"
- The
-m
flag lets you add a concise, meaningful message describing the changes.
Commit Best Practices
- Write clear, descriptive messages. Future-you (and teammates) will thank you.
- Break up large work into small, focused commits. This makes history easier to read and debug.
Amending a Commit
If you forgot to stage a file or want to adjust your last commit message:
$ git add missed-file.js
$ git commit --amend -m "Update header and fix alignment"
Connecting It All
Here’s a typical workflow integrating these commands:
# 1. Make changes to your files
# 2. Check what’s changed
git status
# 3. Stage desired changes
git add app.js styles.css
# 4. Commit the staged changes
git commit -m "Update app and styles for new layout"
If you made additional changes after your commit, repeat the process.
Recap: Effective Change Tracking
- Use
git status
to monitor your working directory and staging area. - Use
git add
to select specific files or chunks to stage for the next commit. - Use
git commit
to record a snapshot of staged changes.
Practicing these steps ensures a well-documented project history, easier collaboration, and a safer way to experiment with code.
Final Thoughts
Git’s flexibility comes from its ability to let you control precisely how and when you record changes. By getting comfortable with git status
, git add
, and git commit
, you set yourself up for a successful and organized development workflow. Start practicing these commands with your next project, and watch your version control confidence grow!
Further Reading:
Happy coding!