Basics
Git Add
Staging Changes
Git add stages changes for commit with file or pattern selection.
Understanding Git Add
Git add is a fundamental command in Git, used to stage changes in your working directory for a commit. It allows you to specify which files you want to include in your next commit, providing precise control over your version history.
Basic Usage of Git Add
The basic syntax of the git add
command is as follows:
Here,
can be a specific filename or a path to a directory. When using a directory path, Git adds all files within the directory and its subdirectories.
Staging Multiple Files
To stage multiple files, you can list them one by one or use a wildcard pattern. For example:
Staging All Changes
To stage all changes in the working directory, use the .
or -A
option:
The .
option stages changes in the current directory and its subdirectories, while -A
stages all changes in the repository, including deletions.
Checking the Staged Changes
After staging changes with git add
, you can verify what's staged by using:
This command shows a list of all files with changes staged for commit, providing a chance to review your changes before committing them.
Common Mistakes with Git Add
One common mistake is assuming git add
stages changes permanently. Changes must be committed to be saved in the repository. Additionally, using git add
without specifying a file or pattern might lead to unintended files being staged.
Conclusion
The git add
command is an essential part of the Git workflow. By understanding how to stage changes effectively, you can maintain a clean and organized version history, allowing for easier collaboration and tracking of your project's evolution.