This guide will explain what it means for a file to be tracked, how to add tracked files, and cover various examples involving tracked and untracked files in Git.
What does it mean for a file to be tracked?
In Git, a tracked file is any file that has been previously staged or committed to your repository history. Git monitors these files for changes. When you modify a tracked file, Git recognizes it as modified but not yet staged for the next commit. In order to stage the changes you've made you need to "add" them to the the staging area. This is the location Git uses as the state in-between your local directory, the code stored on your machine, and the remote repository, the code hosted on the server.
Untracked files, on the other hand, are those that are present in your directory but have never been added to your Git repository. These could be new files you've created that you haven't committed or staged yet. These files exist on your local machine, but Git has no knowledge of their contents or history.
Adding all tracked files
To add all tracked files to the staging area in Git, you can use the following command:
git add -u
The -u
option tells Git to update the staging area with all changes made to tracked files (this includes modifications and deletions, but not new files). This is useful for preparing to commit changes made to files already known to your Git repository. This option will ensure Git does not affect any untracked files.
The only files that will be added are the tracked files that were modified. If there are tracked files that don't have any changes, they will be unaffected by the git add
command.
Scenario-specific commands
1. Adding all tracked and untracked files
To add both tracked modifications and all new untracked files, you can use:
git add .
This command stages all changes (including new files, modified files, and deletions) in the current directory and its subdirectories.
Note that git add .
stages new and modified files in the current directory and its subdirectories to the staging area, but it does not stage deleted files unless explicitly specified. In contrast, git add --all
stages all changes including new, modified, and deleted files across the entire repository, not just the current directory.
2. Adding only tracked files in a specific folder
For more specificity, especially in larger repositories where you only want to add modified files in a specific folder, use:
git add -u <folder_path>
This adds only the modified tracked files in the specified folder, leaving the rest untouched.
3. Adding only untracked files
To add only untracked files without adding any modifications to tracked files, you can use a combination of commands to filter out only the untracked files:
git ls-files --others --exclude-standard | xargs git add
This sequence uses git ls-files
to list all of the untracked files in your local directory, filtering them with --exclude-standard
to avoid adding files ignored by .gitignore
, then pipes them to git add
via xargs.
You can also accomplish the same thing interactively using git add -i
. Once in the interactive prompt, choose a
which stands for "add untracked", then *
to choose every file, then q
to exit the operation.
4. Adding all modified, deleted, and untracked files
To stage all changes in your working directory including new files, modified files, and deleted files, use:
git add -A
or equivalently,
git add --all
These commands are the most comprehensive for staging, as they indiscriminately add all changes to the staging area.
5. Adding all files with specific extensions
If you want to add all files with a specific extension within your project, use:
git add *.<extension>
For example, to add all Python files:
git add *.py
For further reading on adding files in Git, see the official documentation.