Git How to Add All Files (in Folder, by Extension, and More)

When developing software, it’s quite a fundamental necessity to be able to add new files to the projects. When you’re versioning the software using Git, you can use the git add command to start versioning the newly added files, changes, and such.

To add all the untracked files and changes in Git, use the git add . command.

$ git add .

But if you’re in a subdirectory of the project, this command doesn’t add changes in the parent folders.

To overcome this, run git add -A:

git add -A

This is a comprehensive guide to adding all files in Git.

After reading this guide, you know how to add all files in the current directory as well as in the entire project. Besides, you learn how to add all files with a particular extension. Last but not least, you’ll also learn how to add all modified and deleted files but leave new ones alone.

Let’s jump into it!

Adding All Files in Git

There are two main ways for adding all the files (and changes) in Git.

  1. Use the “git add .” command to add files and changes in the current folder and its subfolders.
  2. Use the “git add -A” to add files and changes everywhere in the repo.

Let’s take a closer look at how these commands work and what’s their difference.

The “git add . ” Command

To add new, modified, and deleted files, run the git add . command. Assuming you’re in the root folder of your project, this command adds all the files in the current folder and its subdirectories.

The dot at the end of the command tells Git you want to the changes in the current folder. If you’re familiar with file paths in operating systems, I’m sure you’re familiar with the “.” as it refers to the current working directory. The idea is exactly the same in Git.

Most of the time “git add .” is the command you’re looking for to stage all the files and changes.

Example

Let’s take a look at a concrete example. In this example, I have a Git project in which I’ve added a file, deleted a file, and modified a file.

Here’s what the git status looks like:

Now, let’s add all these changes to the staging area with the git add . command.

Running git status again shows that all the added, modified, and deleted files were added to the staging area.

Notice that if you’re not in the root directory of your Git project, adding with git add . might not work as you’d expect.

The “git add -A” Command

A more aggressive version of the git add . command is git add -A. The git add -A command adds all the new, changed, or deleted files in the entire project regardless of where you’re calling the command from.

If you’re working in the root folder of your project, git add . and git add -A work the same way. But if you’re working in a subfolder of the project and want to add all the changes, you need to use git add -A.

Example

Let’s see a concrete example to understand how the git add -A command works.

Here’s my project setup. The samples/ folder is a newly added folder and it has a file called example.py in it.

When changing the current directory to the /samples folder, the git status shows a report like this:

Now, let’s say I want to add both the new folder (with the example.py file) as well as the change in the parent folder’s test.txt file.

First, let’s see what happens by running git add . you saw earlier:

$ git add .

This only added the example.py file I’ve added to the new subfolder I’m currently working in. But it did not add the change to the parent folder’s test.txt file.

This is because as you learned, the git add . only adds the changes in the current working directory as well as its subdirectories. But it doesn’t add changes in the parent folders.

Now let’s use git add -A instead:

git add -A

Then let’s run git status again. This time, you can see all the files being added in both the samples/ subfolder as well as the parent folder:

Make sure to read my complete guide about “git add .” vs “git add -A”.

Add All Files by File Extension

In Git, you can use the git add command to add all files that have a specific extension. This is handy if you want to add a handful of files of a particular type.

To add all files by a specific extension in Git, specify the file extension after git add as *.extension.

git add *.extension

Example

Let’s take a look at a concrete example of adding all the files that have an extension “.py“.

At the moment, the git status shows there are two unstaged .py files and one unstaged .txt file.

Let’s only add the .py files:

git add *.py

Then, let’s check the git status again:

This shows how you’ve successfully added the .py files but not the .txt file.

Add All Deleted and Modified Files Only

Another useful way to use the git add command is to add only modified and deleted files but not new ones.

To do this, use the -u option in git add.

git add -u .

Example

Let’s see a concrete example.

Here’s the status of my current project:

There’s one new file, one modified file, and a deleted file.

Now, let’s only add the modified and the deleted file to the staging area:

git add -u .

Let’s re-check the status of the project. This shows you how the new file remains untracked, but the modified and deleted files are now staged.

Summary

Today you learned how to add all files in Git.

To take home:

  • git add . adds all the files in the current directory (and its subdirectories)
  • git add -A adds all the files on the entire project, even in parent directories.
  • git add *.extension adds all files that end with .extension.
  • git add -u . adds all the modified and deleted files but not new ones.

Thanks for reading. Happy coding!

Read Also