336

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?

AndresM
  • 1,293
  • 10
  • 19
Ashish Agarwal
  • 14,555
  • 31
  • 86
  • 125
  • 5
    are there files anywhere? aren't they ignored in `.gitignore`? – CharlesB Jul 19 '13 at 10:21
  • 1
    `This is pushing only the first folder` - git doesn't care about folders at all, only files. please show the commit - you've either committed a submodule, a symlink or something else that's not a folder – AD7six Jul 19 '13 at 10:26
  • 1
    I'm having this same problem. I don't think this is a problem but just the expected behavior. Surprising there are so many solutions. –  Sep 06 '15 at 21:26
  • 1
    note: when your folders are empty they are not tracked. its a good practice to put a empty .keep file in there to keep the empty directories – Dude Nov 24 '16 at 13:04

15 Answers15

407

Check the .gitignore file, if the subdirectory is ignored.

Then try again

git add --all
git commit -am "<commit message>"
git push
nbro
  • 15,395
  • 32
  • 113
  • 196
beyersdorfinger
  • 4,192
  • 1
  • 12
  • 3
  • 5
    Note that this will not include files mentioned in .gitignore. I usually add those by hand or use a batch file like this: `for /R %%f in (*.*) do git add --force %%f` (see https://bitbucket.org/jeroenp/besharp.net/src/tip/Scripts/GIT/git-add-force-all-files-recursively-from-current-directory.bat) – Jeroen Wiert Pluimers Oct 28 '13 at 09:49
  • 14
    To recursively add the entire tree structure of the folder, the command should be `git add --all :/` – Jason Hartley Oct 14 '14 at 00:35
  • 10
    @JeroenWiertPluimers `git add -f -all` works to add .gitignored files – Orwellophile Nov 28 '16 at 13:11
  • 4
    @JasonHartley Your comment could be an answer. Thanks! – ShutUpMagda Jan 21 '17 at 16:33
  • 12
    Doesn't this add all unstaged files in the git repo, not just the current folder? I don't think it's what the original question is asking. – Ted May 03 '17 at 18:36
  • 6
    I used this while in the directory that I wanted to start adding files and it ended up adding all the files even outside of the directory. Stay away from this solution. – Johann Mar 07 '18 at 15:30
  • 5
    This doesn't answer the question. It's about adding a folder, not the whole directory tree. Some people don't want to add all because you can accidentally commit unwanted files that way. – Matt Nov 05 '18 at 17:05
  • 2
    This is a bad solution as other commenters mention. This recursively stages all files in the repo, not the current directory from the command line. – FreyGeospatial Jul 21 '22 at 15:08
85

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
Jehad Nasser
  • 3,227
  • 5
  • 27
  • 39
Jake Bantug
  • 867
  • 6
  • 2
45

This worked for me:

git add . --force
ykay
  • 1,877
  • 19
  • 24
  • 2
    The other answers require the file list to be computed as an argument to the command, this one doesn't, hence is a better choice. – topkara Sep 01 '17 at 18:56
  • 5
    Definitely the best answer.. One edit would be to mention that you don't need the `--force` if you want the ignores to still be respected. – GrafikRobot Apr 25 '18 at 21:37
21

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.

disperse
  • 1,216
  • 10
  • 22
18

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.

nopsoft
  • 922
  • 7
  • 10
10

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.

Jaime Montoya
  • 6,915
  • 14
  • 67
  • 103
  • 1
    This can fail. See https://stackoverflow.com/questions/12084227/gitignore-fatal-no-files-added – kmiklas Jul 29 '20 at 15:58
  • 1
    @kmiklas Interesting. Should it be `git add app/src/release/.`? – Jaime Montoya Jul 29 '20 at 19:14
  • 1
    ``git add --all app/src/release/`` worked for me; ref: https://git-scm.com/docs/git-add – kmiklas Jul 29 '20 at 19:32
  • 1
    @kmiklas I like that approach. I will use it next time. It worked for me using `git add app/src/release/*` but if it can fail, why taking risks? I will better use `git add --all app/src/release/` next time. Thank you! – Jaime Montoya Jul 29 '20 at 20:58
8

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.

user2989397
  • 129
  • 1
  • 5
8

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
DanielBarbarian
  • 5,093
  • 12
  • 35
  • 44
user7205168
  • 97
  • 1
  • 1
6

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.


N a y y a r
  • 339
  • 1
  • 6
  • 18
5

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

Atul
  • 3,778
  • 5
  • 47
  • 87
Austine Gwa
  • 804
  • 1
  • 9
  • 18
5

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>/*
Suyash Jain
  • 561
  • 7
  • 18
3

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.

mr5
  • 3,438
  • 3
  • 40
  • 57
3

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

  1. https://git-scm.com/docs/git-add
kmiklas
  • 13,085
  • 22
  • 67
  • 103
1

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
David Buck
  • 3,752
  • 35
  • 31
  • 35
Norfindel
  • 11
  • 1
-2

This worked for me

git add -f *
Amine Sehaba
  • 122
  • 6