Read Anthropic’s case study about Graphite Reviewer

How to add folders in Git

Greg Foster
Greg Foster
Graphite software engineer
Try Graphite


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.

When working with Git to add directories, the Graphite CLI can simplify operations and integrating them into stacked pull requests. Specifically, the commands gt add and gt modify streamline adding or modifying directories and their contents.

The gt add command automatically stages changes with additional conveniences, particularly when managing directories:

Example: Adding a new directory and all its contents

Terminal
# Automatically stage a directory and prepare it for a PR
gt add new_folder --all

This command:

  • Stages the specified directory and all files and subdirectories it contains.
  • Prepares the staged content for inclusion in a stack or pull request.

For granular control, you can specify file types or individual files using Graphite's advanced flags:

Terminal
# Add only text files in a directory
gt add new_folder --pattern "*.txt"

When addressing feedback or making updates to directories already part of a stack, gt modify allows for seamless amendments without breaking the structure of your pull requests.

Example: Making changes to a folder in a stack

Terminal
# Amend the latest commit in your stack with changes to a folder
gt modify new_folder --all

This command:

  • Automatically applies changes to the specified directory.
  • Restacks any dependent branches to ensure consistency across the stack.

For more explicit control, you can use --commit to create a new commit for your modifications:

Terminal
# Create a new commit for changes to the folder
gt modify new_folder --commit --all --message "Updated new_folder contents"
  1. Stack-aware modifications: Automatically updates related pull requests when you add or modify directories.
  2. Simplified Git commands: Reduces the complexity of traditional Git commands, especially for staging and amending.
  3. Efficiency: Speeds up iterative development workflows, ensuring consistency and reducing manual error.

By incorporating the Graphite CLI, you can manage directory operations while keeping your workflow fast, stack-aware, and tightly integrated with your pull requests. This approach is especially helpful in scenarios involving complex directory structures or frequent feedback cycles.

Built for the world's fastest engineering teams, now available for everyone