codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
📦

Git & GitHub

5 / 63 topics
1Installing Git2Configuring Git3Initializing a Repository4Using git status5Using git add6Using git commit7Using git log8Using git diff
Tutorials/Git & GitHub/Using git add
📦Git & GitHub

Using git add

Updated 2026-04-20
3 min read

Using git add

Introduction

git add is a fundamental command in Git that stages changes for commit. It's essential for preparing your work before creating a new commit. This tutorial will cover how to use git add, including its syntax, options, and best practices.

Basic Syntax

The basic syntax of the git add command is:

git add <file>...

You can also stage multiple files at once:

git add file1.txt file2.txt

Or use a wildcard to stage all files in a directory:

git add path/to/directory/*

Staging All Changes

To stage all changes in the working directory, including new files and modified ones, use the -A or --all option:

git add -A

Or:

git add --all

This command stages everything that needs to be committed.

Staging Modified Files

To stage only modified (but not new) files, you can use:

git add -u

Or:

git add --update

This is useful when you want to commit changes without adding new files.

Interactive Staging

Git provides an interactive mode for git add that allows you to selectively stage parts of a file. To use it, run:

git add -p

Or:

git add --patch

This will prompt you to choose which hunks (sections) of the files you want to stage.

Staging Specific Lines

For even more granular control, you can stage specific lines within a file. This is particularly useful for large changes where only certain parts need to be committed. Use:

git add -p <file>

This will break down the file into hunks and allow you to select which lines to stage.

Unstaging Files

If you accidentally stage a file that you don't want to commit, you can unstage it using git reset:

git reset HEAD <file>

Or for multiple files:

git reset HEAD file1.txt file2.txt

This command will remove the file from the staging area but keep the changes in your working directory.

Staging with Patterns

You can also use patterns to stage specific types of files. For example, to stage all .txt files:

git add '*.txt'

Or to exclude certain files:

git add '*.[ch]' '!lib/*.[ch]'

This will stage all C and header files except those in the lib directory.

Best Practices

  1. Stage Only Relevant Changes: Always review your changes before staging them. Use git diff --cached to see what's staged.

  2. Use Interactive Mode for Complex Changes: When dealing with large or complex changes, use interactive mode to stage only necessary parts.

  3. Commit Often and in Small Batches: Smaller, more frequent commits make it easier to track changes and revert if needed.

  4. Keep Your Staging Area Clean: Regularly unstage files that are not ready for commit to keep your working directory organized.

  5. Use Patterns Wisely: While patterns can be powerful, they should be used carefully to avoid accidentally staging unintended files.

Example Workflow

Here's a step-by-step example of using git add in a typical workflow:

  1. Make Changes:

    echo "New feature" >> feature.txt
    
  2. Stage the Change:

    git add feature.txt
    
  3. Review Staged Changes:

    git diff --cached
    
  4. Commit the Change:

    git commit -m "Add new feature"
    

Conclusion

git add is a versatile command that plays a crucial role in Git workflows. By understanding how to use it effectively, you can manage your changes more efficiently and ensure that your commits are clear and meaningful.

Remember, practice makes perfect. The more you use git add, the more comfortable you'll become with its various options and capabilities. Happy coding!


PreviousUsing git statusNext Using git commit

Recommended Gear

Using git statusUsing git commit