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.
Step 1: Add the folder to the staging area
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:
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:
git add new_folder/example.txt
Step 2: Check the status
To ensure your files are staged, run:
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.
Step 3: Commit the changes
Once your new folder and its contents are staged, commit them to your repository:
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.
Additional commands and options
Adding an entire directory: To add everything in the directory, including all folders and subfolders, run:
Terminalgit 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:Terminalgit add folder1 folder2Adding folders with all files: The commands shown above add folders along with all their contents. Use wildcards for more granular control, for example:
Terminalgit 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.