Skip to main content

Git Add

The git add command is used after checking the status of the files, to add new or changed files in your directory to the staging area. The staging area contains a list of all the files you have recently changed. When the git commit command is run, by default it only looks at this staging area, so git add is used to craft what exactly you would like your next commit snapshot to look like.

The git add command can also serve two other purposes

  • To Resolve basic merge conflicts
  • To interactively stage only specific parts of a modified file in Interactive Staging.

Git Add Syntax:

git add [file]

Git Add Common Usage


  • git add <path>: Stage a specific directory or file.

    Example:

    touch newfile.txt # Create a new file in the git repository
    git add newfile.txt


  • git add . (or) git add -A: Stage all the files available in the repository. Instead of adding one file at a time, this command allows users to add all the available files at once.

    Example:

    touch newfile1.txt # Create a new file in the git repository
    touch newfile2.txt
    touch newfile3.txt
    git add -A


  • git add --ignore-removal: Update the index by adding new files that are unknown to the index and files modified in the working tree, but ignore files that have been removed from the working tree.


  • git add -u: Stage only the modified and deleted files. It will not stage the newly created files.

  • git add *.md: Add all the same pattern files at once. It is another way to add multiple files together. This command will stage all the markdown files.

  • git add -p: Begin an interactive staging session that lets you choose portions of a file to add to the next commit.

Undo Git Add Operation

We can undo a git add operation. However, it is not a part of git add command. We can do it through git reset command.

To undo an add operation, run the below command:

git reset <file-name>

You can learn more about the git add command and its options in git-scm's documentation.