Git Stage
At its core, the git add
command is responsible for staging changes—marking
modifications, deletions, or additions to files in the working directory as candidates for
the next commit. Let's delve into the fundamental ways to utilize this command.
1. Staging Specific Files:
The most basic use of git add
involves specifying individual files to stage.
This is ideal when you want to be selective about the changes to include in the next commit.
bashgit add filename.txt
2. Staging All Changes:
To stage all changes in the working directory, you can use the dot (.
) as a
wildcard, indicating all files and directories.
bashgit add .
3. Staging Changes Interactively:
For a more interactive approach, the -p
or --patch
option allows
you to review each change and decide whether to stage it or not.
bashgit add -p
4. Staging Portion of a File:
With the --patch
option, you can stage only specific portions of a file,
enabling precision in selecting the changes to include.
bashgit add -p filename.txt
5. Staging New and Modified Files:
Combining the -u
or --update
option with git add
stages both modified and untracked (new) files.
bashgit add -u
6. Staging Changes Excluding New Files:
Conversely, the -A
or --all
option stages all changes but omits new
files. This is useful for excluding untracked files from the staging area.
bashgit add -A
7. Staging Files with a Specific Extension:
You can stage files with a specific extension using the following command, replacing
.txt
with your desired extension.
bashgit add *.txt
8. Staging Changes Without Committing:
To stage changes without immediately committing them, you can use git add
followed by git commit
.
bashgit add .
git commit -m "Your commit message"