0
0
GitHow-ToBeginner · 3 min read

How to Add a Specific File in Git: Simple Commands Explained

To add a specific file in Git, use the command git add <filename>. This stages the file so it will be included in the next commit.
📐

Syntax

The basic syntax to add a specific file in Git is:

git add <filename>

Here:

  • git add is the command to stage files.
  • <filename> is the exact name of the file you want to add.
bash
git add filename.txt
💻

Example

This example shows how to add a file named notes.txt to the Git staging area.

bash
echo "My notes" > notes.txt
git add notes.txt
git status
Output
On branch main Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: notes.txt
⚠️

Common Pitfalls

Common mistakes when adding specific files include:

  • Typing the wrong filename or path, causing Git to show an error.
  • Forgetting to add the file before committing, so changes are not saved.
  • Using git add . by mistake, which adds all files instead of just one.

Always double-check the filename and use git status to confirm staged files.

bash
git add wrongfile.txt  # Wrong filename
# Correct way:
git add correctfile.txt
📊

Quick Reference

Here is a quick cheat sheet for adding files in Git:

CommandDescription
git add filename.txtAdd a specific file to staging
git add .Add all changed files in current directory
git statusShow staged and unstaged changes
git commit -m "message"Commit staged changes with a message

Key Takeaways

Use git add <filename> to stage a specific file for commit.
Always verify the filename and path before adding to avoid errors.
Check staged files with git status before committing.
Avoid using git add . if you want to add only one file.
Committing without adding files first will not save your changes.