Git Add

git add is the command that stages changes from your working directory into Git’s index, marking them to be included in the next commit. Staging is an intermediate step that gives you precise control — you can add one file at a time, leaving other modified files unstaged until a later commit. This is the third post in this site’s Git command guide, following Git Clone.

Basic Usage

Suppose you made changes to two files named file1.txt and file2.txt. You want to include the changes to file1.txt in your next commit, but not the changes to file2.txt. First navigate to the project directory in the terminal and execute:

git add file1.txt

This command stages the changes made to file1.txt. You can now review what’s staged by running:

git status

You should see that file1.txt has been staged. Now you can commit the staged changes by running:

git commit -m "Added new feature to file1.txt"

The commit will include only the changes to file1.txt since file2.txt was not staged.

How git add Works

  • git add <file> copies the current state of that file from the working directory into the staging area (also called the index). Git will use this staged snapshot — not the live file — when you run git commit.
  • If you modify the file again after staging it, the new changes won’t be included in the next commit unless you run git add again.
  • To stage every changed and new file at once, use git add . — covered in the next post, Git Add . (All).

See Also: This Site’s Git Command Guide

This post is part of a 13-part Git command guide on this site:

Conclusion

git add is the step between making a change and recording it. Getting comfortable staging individual files — rather than always using git add . — gives you cleaner, more intentional commits. Next up: staging everything at once with Git Add . (All).

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.