How to recursively add a folder in Git?
Sometimes, when working on a project, a developer may need to add a folder recursively (with all its content) to a Git repository. But how do you do it?
To recursively add a folder to a Git repository you can:
- Use the --all flag.
- Use the --force flag.
- Use a regex.
This article explains those solutions with code snippets.
Let's get to it 😎.
⚠️ The first thing you need to do, is to check the .gitignore file (if it exists) and verify that the folder you are trying to add is NOT ignored. ⚠️
Solution #1 - Use the --all flag
One way to recursively add a folder to a Git repository is to use the git add command with the --all flag.
Here is how to do it:
bashgit add --all
Then, you can commit and push your changes.
Note: If the folder you want to add is empty, you need to add a gitkeep file inside.
Solution #2 - Use the --force flag
Another way to recursively add a folder to a Git repository is to use the git add command with the --force flag.
Here is how to do it:
bashgit add . --force
After, commit and push your changes to a remote Git service (for example, Github, GitLab, or BitBucket).
Solution #3 - Use a regex
One more way to recursively add a folder to a Git repository is to use the git add command with a regex.
Here is how to do it:
bashgit add folder_name/\*
Final thoughts
As you can see, it is simple to recursively add a folder to a Git repository.
Most of the time, the --all flag will do the job for you.
Here are some other Git tutorials for you to enjoy: