0
0
Gitdevops~5 mins

git add for staging files - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you make changes to files in a project, you need to tell Git which changes you want to include in the next snapshot. The git add command lets you pick these changes and prepare them for saving.
When you have edited files and want to include those changes in your next commit.
When you want to add new files to your project so Git starts tracking them.
When you want to remove files from the project and prepare that removal for the next commit.
When you want to selectively choose some changes but not others before saving.
When you want to check what files are staged before committing.
Commands
This command stages the file named example.txt, telling Git to include its current changes in the next commit.
Terminal
git add example.txt
Expected OutputExpected
No output (command runs silently)
This stages all changed files in the current folder and its subfolders, preparing all your edits for the next commit.
Terminal
git add .
Expected OutputExpected
No output (command runs silently)
Shows which files are staged and which are not, so you can verify what will be saved in the next commit.
Terminal
git status
Expected OutputExpected
On branch main Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: example.txt Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: README.md
Starts an interactive mode where you can choose which parts of changed files to stage, useful for fine control.
Terminal
git add -p
Expected OutputExpected
diff --git a/example.txt b/example.txt index e69de29..d95f3ad 100644 --- a/example.txt +++ b/example.txt @@ -0,0 +1,2 @@ +Hello +World Stage this hunk [y,n,q,a,d,e,?]?
-p - Interactively choose chunks of changes to stage
Key Concept

If you remember nothing else from this pattern, remember: git add prepares your changes so Git knows exactly what to save in the next commit.

Common Mistakes
Running git commit without running git add first
Git will commit only the changes that were staged before, so new edits won't be saved.
Always run git add on the files you want to include before committing.
Using git add . without checking what files are staged
You might accidentally stage unwanted files or changes, leading to messy commits.
Use git status to review staged files before committing.
Trying to stage deleted files without using git add
Git won't track file removals unless you stage them explicitly.
Use git add on deleted files or use git rm to stage removals.
Summary
git add stages changes so Git knows what to include in the next commit.
You can stage individual files, all files, or parts of files with git add.
Use git status to check which files are staged before committing.