W3docs

git add

See the detailed information about the git add command, the working principles with examples, common options, such as interactive mode and editing patches.

Description

The git add command adds changes in the working directory to the staging area. With its help, you tell Git that you want to include updates to a specific file in the next commit. However, to record these changes, you must also run git commit. Combined with the commands mentioned above, the git status command is also useful for checking the state of the working directory and the staging area.

gitadd

Working principles

The git add and git commit commands are used to record project versions in the repository history. They are essential for every Git user, as they form the basis of the Git workflow.

While developing a project, you first edit files in the working directory. When a copy of the current state is ready to save, you stage the changes using git add. The git add command should be run every time you alter a file.

Staging area

The basic role of the git add command is promoting changes in the working directory to the Git staging area. The staging area of Git is one of its most special characteristics, which lets you gather all the connected changes into highly focused snapshots. Only after that, you can commit these changes to your project history.

Common options

  1. Staging changes in <file> for the next commit:

git add file

git add <file>
  1. Staging all changes in <directory> for the next commit:

git add directory

git add <directory>
  1. Staging all changes in already tracked files:

git add -u

git add -u
  1. Interactive patch mode (staging changes hunk by hunk):

git add -p

git add -p

Examples of git add

The git add and git commit commands are used to create an initial commit of the current directory, as well as to record changes to existing files.

After starting a project, you can add new files by passing their path to git add.

Add a file

git add hello.py

Commit the changes

git commit

Interactive mode

An interactive staging session lets you choose portions of a file to be added to the next commit. You will be shown a chunk of changes and prompted for a command. The available options are:

  • y - stage the hunk
  • n - do not stage the hunk
  • q - quit; do not stage the hunk or any remaining ones
  • a - stage the hunk and all later hunks in the file
  • d - do not stage the hunk or any later hunks in the file
  • g - select a hunk to go to
  • / - search for a hunk matching the given regex
  • j - go to the next hunk
  • J - go to the previous hunk
  • k - go to the previous hunk
  • K - go to the previous undecided hunk
  • s - split the current hunk into smaller ones
  • e - manually edit the current hunk
  • ? - print help

Editing patches

Calling git add -e or selecting e from the interactive chunk selector opens a patch in your editor. After exiting the editor, the output is applied to the index. You can make arbitrary changes to the patch, but some edits may result in complex outputs or even make the patch inapplicable. If you want to decline the operation entirely, simply delete all lines of the patch. Here are some common elements you may see in a patch, and which editing operations make sense for them.

Lines starting with + represent added content. You can delete them to prevent staging any added lines.

Lines beginning with - represent removed content. To prevent staging their deletion, you can convert the - to a space ( ).

Modified content is shown with - lines (deleting the old content) followed by + lines (adding the replacement content). To prevent staging the modification, convert the - lines to spaces and remove the + lines. Note that modifying only half of the pair may cause confusing changes to the index.

Practice

Practice

What are the functionalities and options associated with the 'git add' command?