348

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?

4
  • 5
    are there files anywhere? aren't they ignored in .gitignore?
    – CharlesB
    Commented Jul 19, 2013 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
    Commented Jul 19, 2013 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.
    – user2258887
    Commented Sep 6, 2015 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
    Commented Nov 24, 2016 at 13:04

15 Answers 15

412

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

Then try again

git add --all
git commit -am "<commit message>"
git push
8
  • 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 bitbucket.org/jeroenp/besharp.net/src/tip/Scripts/GIT/…) Commented Oct 28, 2013 at 9:49
  • 14
    To recursively add the entire tree structure of the folder, the command should be git add --all :/ Commented Oct 14, 2014 at 0:35
  • 10
    @JeroenWiertPluimers git add -f -all works to add .gitignored files Commented Nov 28, 2016 at 13:11
  • 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
    Commented May 3, 2017 at 18:36
  • 7
    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
    Commented Mar 7, 2018 at 15:30
93

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
5
  • 11
    Failed as fatal: pathspec 'foldername/\\*' did not match any files. Worked as git add foldername/\*.
    – Tpojka
    Commented Jan 8, 2018 at 20:49
  • 2
    What's the difference between git add foldername/\\* and git add --all?
    – Danijel
    Commented May 15, 2019 at 13:46
  • 2
    Danijel, the former adds all the files in one folder, whereas the latter adds all files in the repository
    – GuyStalks
    Commented Aug 10, 2020 at 13:33
  • 5
    git add -f foldername/\* - worked for me. Added -f. Commented Feb 26, 2021 at 6:26
  • Thanks Andrey Patseiko. The '-f' specifier also worked for me - SourceTree 4.2.5 (259) on Mac OS.
    – coarist
    Commented Dec 17, 2023 at 2:21
46

This worked for me:

git add . --force
2
  • 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
    Commented Sep 1, 2017 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. Commented Apr 25, 2018 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.

1
  • This solved it for me. I also had to rename the folder itself for the fix to kick in.
    – Totem
    Commented Dec 4, 2016 at 15:34
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.

1
  • 4
    I don't think so, I use git add * and am having the same issue.
    – user2258887
    Commented Sep 6, 2015 at 21:26
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.

4
  • 1
    This can fail. See stackoverflow.com/questions/12084227/…
    – kmiklas
    Commented Jul 29, 2020 at 15:58
  • 1
    @kmiklas Interesting. Should it be git add app/src/release/.? Commented Jul 29, 2020 at 19:14
  • 2
    git add --all app/src/release/ worked for me; ref: git-scm.com/docs/git-add
    – kmiklas
    Commented Jul 29, 2020 at 19:32
  • 2
    @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! Commented Jul 29, 2020 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.

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
1
7

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.


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>/*
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

1
  • 2
    Only answer that mentions init
    – Atul
    Commented Jan 22, 2020 at 9:10
4

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.

2
  • 1
    How can you get around this if you dont want to remove the .git folders?
    – rollsch
    Commented Jan 1, 2019 at 2:56
  • 1
    @rolls see git submodules
    – mr5
    Commented Jan 2, 2019 at 1:36
4

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

  1. https://git-scm.com/docs/git-add
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
-2

This worked for me

git add -f *

Not the answer you're looking for? Browse other questions tagged or ask your own question.