[git] Recursively add the entire folder to a repository

I am trying to add a branch to the master branch on GitHub and push a folder onto that branch.

The folder structure of the branch looks like - SocialApp/SourceCode/DevTrunk/SocialApp and all the source code files are in the last folder.

I am using the following Git commands:

git add *
git commit -m with the message
git push

This is pushing only the first folder "SocialApp" onto GitHub and ignoring the folder SourceCode that is inside the folder. How do I fix this?

This question is related to git github

The answer is


In my case, there was a .git folder in the subdirectory because I had previously initialized a git repo there. When I added the subdirectory it simply added it as a subproject without adding any of the contained files.

I solved the issue by removing the git repository from the subdirectory and then re-adding the folder.


I just needed to do this, and I found that you can easily add files in subdirectories. You only need to be on the "top directory" of the repo, and then run something like:

$ git add ./subdir/file_in_subdir.txt

This worked for me:

git add . --force

Scenario / Solution 1:
Ensure your Folder / Sub-folder is not in the .gitignore file, by any chance.


Scenario / Solution 2:
By default, git add . works recursively.


Scenario / Solution 3:
git add --all :/ works smoothly, where git add . doesn't (work).
(@JasonHartley's comment)


Scenario / Solution 4:
The issue I personally faced was adding Subfolders or Files, which were common between multiple Folders.

For example:
Folder/Subfolder-L1/Subfolder-L2/...file12.txt
Folder/Subfolder-L1/Subfolder-L2/Subfolder-L3/...file123.txt
Folder/Subfolder-L1/...file1.txt

So Git was recommending me to add git submodule, which I tried but was a pain.


Finally what worked for me was:

1. git add one file that's at the last end / level of a Folder.
For example:
git add Folder/Subfolder-L1/Subfolder-L2/Subfolder-L3/...file123.txt

2. git add --all :/ now.
It'll very swiftly add all the Folders, Subfolders and files.



I simply used this:

git add app/src/release/*

You simply need to specify the folder to add and then use * to add everything that is inside recursively.


Navigate to the folder where you have your files
if you are on a windows machine you will need to start git bash from which you will get a command line interface then use these commands

git init   //this initializes a .git  repository in your working directory

git remote add origin <URL_TO_YOUR_REPO.git> // this points to correct repository where files will be uploaded

git add *   // this adds all the files to the initialialized git repository

if you make any changes to the files before merging it to the master you have to commit the changes by executing

git commit -m "applied some changes to the branch"

After this checkout the branch to the master branch


If you want to add a directory and all the files which are located inside it recursively, Go to the directory where the directory you want to add is located.

$ cd directory
$ git add directoryname

I ran into this problem that cost me a little time, then remembered that git won't store empty folders. Remember that if you have a folder tree you want stored, put a file in at least the deepest folder of that tree, something like a file called ".gitkeep", just to affect storage by git.


I also had the same issue and I do not have .gitignore file. My problem was solved with the following way. This took all sub-directories and files.

git add <directory>/*

SETUP

  • local repository at a local server
  • client is connected to the local server via LAN

UPDATE(Sep 2020): use foldername/\* instead of foldername/\\*:

git add foldername/\*

To make it to the server...

git commit -m "comments..."
git push remote_server_name master

Mostly, users will assign remote_server_name as origin...

git remote add remote_server_name username@git_server_ip:/path/to/git_repo

Both "git add *" and "git add SocialApp" called from top directory should add recursively all directories.

Probably you have no files in SocialApp/SourceCode/DevTrunk/SocialApp and this is the reason.

Try to call "touch SocialApp/SourceCode/DevTrunk/SocialApp/.temporary" (and check .gitignore) and then try git add again.


This worked for me

git add -f *

git add --all my/awesome/stuff/ works for me. [1]

  1. https://git-scm.com/docs/git-add

There are times that I want to include my web service source codes along with its client-side project. Both of them have a separate git repositories. I am actually used to add all files using the command:

git add -A

But for some reason, it only adds the folder. Later on I found out that the server files also have its .git folder in it so the command doesn't work.

tl;dr: Make sure there are no .git folder inside the folder you want to stage.