Git tracks changes in files and folders in your project directory. When you add or modify files inside a folder, Git treats them as part of your commit. Committing a folder is essentially committing the changes in all files within that folder.
This guide explains how to commit a folder (and its contents) to your Git repository.
Step 1: Check Your Folder and Files
Make sure your folder contains the files you want to commit. Git only tracks files, not empty folders.
Step 2: Add the Folder to the Staging Area
Use the git add
command with the folder path to stage all files inside it:
git add path/to/your/folder
Example:
git add src/
This stages all changes (new, modified, deleted files) inside the src
folder.
Step 3: Commit the Changes
Once staged, commit the changes with a descriptive message:
git commit -m "Add/update files in src folder"
Step 4 (Optional): Verify Your Commit
To confirm your folder’s files are committed, you can run:
git status
After committing, this should show a clean working directory (no changes).
To see your commit history:
git log --stat
Important Notes
- Git doesn’t track empty folders. To include an empty folder, add a placeholder file like
.gitkeep
. - You can add multiple folders or files at once, e.g.,
git add folder1 folder2 file.txt
. - If you want to add all changes in your repo (including all folders), use
git add .
orgit add -A
.
Summary
Step | Command |
---|---|
Stage a folder | git add path/to/folder |
Commit staged files | git commit -m "Your message" |
Check status | git status |
View commit history | git log --stat |
Committing a folder in Git is just about staging and committing all files within it. This keeps your project organized and your version history clean.