How to Add Folder in Git

Isaac Newton Aranas Feb 02, 2024
  1. Use git add to Add All or Specific Folders and Files to Stage Content in Git
  2. Create Project Folder With 2 Folders and Files to Test git add in Git
  3. Conclusion
How to Add Folder in Git

git add is used to add specific folders and files. This tutorial will tackle git add <folder> in a modern way.

Use git add to Add All or Specific Folders and Files to Stage Content in Git

Add a file using this syntax:

git add <file>

Add a folder using this syntax:

git add folder1/

or

git add folder1

For older versions of git, add the --all flag and add forward slash at the end of the folder name.

git add --all <folder>/

For example:

git add --all folder1/

Create Project Folder With 2 Folders and Files to Test git add in Git

First, create a folder using this command:

mkdir project-folder

To enter the folder, use the bash code.

cd project-folder

Inside the project folder, open Git Bash.

git init

Initialized empty Git repository in C:/You/Documents/project-folder/.git/

This will initialize a git working tree. Create two new folders inside the project folder, and name them folder1 and folder2.

Inside folder1, add a text document and name it text1.txt.

To create a file, run the following command:

touch text1.txt

On Git Bash, run the following code:

git status

On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        folder1/
        folder2/

nothing added to commit but untracked files present (use "git add" to track)

The folder1/ and folder2/ are untracked files and not included in the files and folders which are ready to be committed.

Note
There is no staged file or folder in this sample practice yet.

Say we want to add folder2/ alone. We will add that, while folder1 will be left unstaged.

git add --all folder2/

or

git add folder2

Check the status.

git status

On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   folder2/text1.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        folder1/

The folder2/text1.txt file and folder are now added to the staged contents. The untracked folder is folder1/.

The . also means all but not equivalent with --all.

Run git add . folder2/ instead of git add --all folder2/. We will put back the folder2 to the unstaged and put it staged again to test this.

git restore --staged .

or

git rm --cached folder2/ -r

Let’s check the status;

git status

On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        folder1/
        folder2/

nothing added to commit but untracked files present (use "git add" to track)

Now that all folders are untracked, we can test the code ..

git add . folder2/
git status

On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   folder1/text1.txt
        new file:   folder2/text1.txt

Two of them are added, as . is meant for all files and folders.

So don’t use . and expect to stage-specific folder. Use --all as in git add --all folder2/.

Conclusion

Add folders is most the same as adding files. Now we can run git add <folder> or git add <folder>/ to cherry-pick folder to stage.

Related Article - Git Add