Git add
The git add command stages changes from the working directory to the staging area, preparing them to be included in the next commit. This essential Git command allows developers to selectively choose which modifications should be committed, providing precise control over version history and enabling organized, logical commits in any Git workflow.
Syntax of git add
git add [options] [pathspec]
Common Syntax Variations
git add <filename>
git add .
git add -A
git add --all
Parameters:
<filename>: Specifies the exact file to stage for the next commit.: Stages all changes in the current directory and its subdirectories-Aor--all: Stages all changes in the entire repository, including new, modified, and deleted files-uor--update: Stages only modified and deleted files (excludes new files)-por--patch: Begins interactive staging mode to selectively stage parts of files-ior--interactive: Opens interactive mode for choosing which changes to stage-for--force: Forces staging of ignored files-nor--dry-run: Shows what would be staged without actually staging anything
Return value:
The git add command returns no output on successful execution. If there are errors (such as attempting to stage non-existent files), Git displays appropriate error messages. Use git status to verify which files have been successfully staged.
Example 1: Basic File Staging using git add
This example demonstrates how to stage individual files for the next commit:
# Create a new file in the projectecho "Hello, Git!" > welcome.txt# Check the current statusgit status# Stage the new filegit add welcome.txt# Verify the file is stagedgit status
After running these commands, welcome.txt will be moved from “Untracked files” to “Changes to be committed” in the status output, confirming it’s ready for the next commit.
Example 2: Using git add for Team Collaboration Workflow
This example shows how to stage multiple files when working on a feature with a team:
# Working on a new feature - multiple files modified# Modified files: app.js, style.css, README.md# New file: config.json# Stage specific files for a focused commitgit add app.js config.json# Check what's staged vs unstagedgit status# Stage remaining files for a documentation updategit add style.css README.md# Commit the changes with descriptive messagesgit commit -m "Add user authentication feature"# Push to share with the teamgit push origin feature-branch
This workflow demonstrates how git add enables developers to create logical, focused commits by staging related changes together, making code review and collaboration more effective.
Example 3: Staging All Changes With git add --all
This example demonstrates how to stage all changes in the repository at once using git add --all:
# Working directory with multiple changes:# Modified: index.html, app.js# New files: contact.html, footer.css# Deleted: old-style.css# Check current repository statusgit status# Stage all changes at once (new, modified, deleted)git add --all# Verify all changes are stagedgit status# Alternative shorter syntax (same result)git add -A# Commit all staged changesgit commit -m "Complete website redesign with new contact page"
After running git add --all, all modifications, new files, and deletions will be moved to the “Changes to be committed” section, making this command ideal for comprehensive updates when there is a need to include everything in the next commit.
Frequently Asked Questions
1. How do I add files to git?
Use git add <filename> for single files, git add . for all changes in the current directory, or git add -A for all changes in the repository. Always verify with git status before committing.
2. What to use instead of git add?
There’s no direct replacement for git add since it’s fundamental to Git’s three-stage workflow. However, you can use git commit -a to automatically stage and commit tracked files (but this won’t include new files).
3. Does git add include deleted files?
The basic git add . command does include deleted files. Use git add -A to explicitly include all changes including deletions, or git add -u to stage only modifications and deletions while excluding new files.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Git on Codecademy
- Use our beginner friendly Git course to integrate Git and GitHub and manage versions of your projects using Git branches.
- With Certificate
- Beginner Friendly.4 hours
- Learn about the command line, starting with navigating and manipulating the file system, and ending with redirection and configuring the environment.
- With Certificate
- Beginner Friendly.4 hours