How to add folders in Git

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


Git does not track directories on their own. Instead, it tracks files within directories. So when you add a "folder" to a Git repository, you're really adding the files within that folder. If a folder is empty, Git will not track it until it contains at least one file.

In this guide, we'll explore how to use Git to add folders, including their contents, to your project repository.

To add the new folder along with all its contents to the staging area (the place Git uses as the temporary middle ground between your local working directory and the remote repository), use the git add command followed by the folder name:

Terminal
git add new_folder

This command stages new_folder and all its contents (files and subfolders). If you only want to add specific files or subfolders, specify their paths:

Terminal
git add new_folder/example.txt

To ensure your files are staged, run:

Terminal
git status

This command shows the current status of the repository including staged, modified, and untracked files. Staged files appear under the "Changes to be committed" section.

Once your new folder and its contents are staged, commit them to your repository:

Terminal
git commit -m "Add new folder with example files"

This command will create a new "commit" object in Git which will keep a record of the file creation, the contents of the file at the time of the commit, and other metadata such as the name of the committer, and the time/date of the commit.

The -m flag allows you to add a commit message inline, describing the changes you’ve made.

  • Adding an entire directory: To add everything in the directory, including all folders and subfolders, run:

    Terminal
    git add .

    The period (.) represents the current directory.

  • Adding specific folders: To add multiple specific folders, add each additional folder as an argument to the git add command:

    Terminal
    git add folder1 folder2
  • Adding folders with all files: The commands shown above add folders along with all their contents. Use wildcards for more granular control, for example:

    Terminal
    git add *.txt

This command adds all .txt files in the current directory to the staging area.

For further reading on adding files and folders in Git, see the official Git documentation.

Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2