Git Basics | git add
The git add command
git add is the command for adding changes in the working directory to the staging area. It tells Git to include each file’s updated content in the next commit. However, git add itself does not affect the local repository. Changes are not actually recorded until you run git commit.
The git status command is used with this workflow to check the state of the working directory and staging area.
Usage
git add <file>
Stages all changes made to <file> and includes them in the next commit.
git add <directory>
Stages all changes under <directory> and includes them in the next commit.
git add -p
Starts an interactive staging session. In an interactive staging session, you can select parts of a file to stage for the next commit. The command displays changes and asks for input. Enter y to stage that part, n to ignore it, s to split it into smaller pieces, e to edit it manually, or q to exit the interactive session.
Notes
git add and git commit form Git’s basic workflow. Every Git user should understand these two commands, regardless of the collaboration model used by the team. They are the way to record a project’s history in the version repository.
As the project progresses, the edit, stage, and commit pattern is repeated.
First, you edit files in the working directory.
When you want to save the current state of the working directory, you use git add to stage the changes.
If the staged snapshot looks correct, you use git commit to commit it to the local repository.
Git Tutorial: git add Snapshot
Do not confuse the git add command with the svn add command, which adds files to a repository. git add operates at a more abstract level over changes. In other words, svn add is called once for each file, while git add must be called each time you modify a file. This may sound verbose, but this workflow makes it easier to keep the project well organized.
Staging area
The staging area is one of Git’s distinctive features and can be difficult to understand for people familiar with SVN or Mercurial. You can think of it as a buffer between the working directory and the local repository.
With the staging area, you do not have to commit every change made since the previous commit at once. Instead, you can group only strongly related changes, create a focused snapshot, and then make the actual commit. In other words, you can freely edit files first, organize related changes into the staging area, and commit them in small units to create logically well-structured commits. This is important in any version control system: keep individual commits small so you can minimize the impact on other parts of the project and make it easier to trace and revert bugs.
Example
When starting a new project, git add serves a role similar to svn import. To make the first commit in the current directory, use these two commands:
git add .
git commit
After the project has started, you can add a new file by passing its path to git add:
git add hello.py
git commit
The same two commands are used to record changes to existing files. Git does not distinguish between a change that creates a new file and a change that modifies the content of a file already stored in the repository.