Recursively add the entire folder to a repository
Asked Answered
G

15

344

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?

Gum answered 19/7, 2013 at 10:11 Comment(4)
are there files anywhere? aren't they ignored in .gitignore?Pleuron
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 folderMillhon
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.Subtrahend
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 directoriesPremonitory
D
410

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

Then try again

git add --all
git commit -am "<commit message>"
git push
Doralyn answered 19/7, 2013 at 13:30 Comment(8)
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/…)Toinette
To recursively add the entire tree structure of the folder, the command should be git add --all :/Unreadable
@JeroenWiertPluimers git add -f -all works to add .gitignored filesRingsmuth
@JasonHartley Your comment could be an answer. Thanks!Aceae
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.Edla
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.Crowl
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.Bolitho
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.Reamonn
C
89

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
Cothurnus answered 9/9, 2014 at 10:21 Comment(5)
Failed as fatal: pathspec 'foldername/\\*' did not match any files. Worked as git add foldername/\*.Saltatorial
What's the difference between git add foldername/\\* and git add --all?Swanhildas
Danijel, the former adds all the files in one folder, whereas the latter adds all files in the repositoryHurter
git add -f foldername/\* - worked for me. Added -f.Gnathonic
Thanks Andrey Patseiko. The '-f' specifier also worked for me - SourceTree 4.2.5 (259) on Mac OS.Tellurion
E
46

This worked for me:

git add . --force
Epa answered 11/9, 2014 at 22:21 Comment(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.Crummy
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.Darendaresay
L
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.

Latisha answered 25/8, 2016 at 18:32 Comment(1)
This solved it for me. I also had to rename the folder itself for the fix to kick in.Apanage
P
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.

Petition answered 19/7, 2013 at 10:22 Comment(1)
I don't think so, I use git add * and am having the same issue.Subtrahend
S
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.

Stinko answered 13/6, 2019 at 21:47 Comment(4)
This can fail. See #12084727Rig
@Rig Interesting. Should it be git add app/src/release/.?Stinko
git add --all app/src/release/ worked for me; ref: git-scm.com/docs/git-addRig
@Rig 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!Stinko
I
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.

Improvisation answered 8/1, 2016 at 20:52 Comment(0)
N
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
Neille answered 24/11, 2016 at 12:19 Comment(1)
cd directory git add *Ladle
W
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.


Woodwaxen answered 30/3, 2020 at 1:23 Comment(0)
Y
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

Yockey answered 11/5, 2018 at 8:27 Comment(1)
Only answer that mentions initPistol
M
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>/*
Mauricio answered 19/11, 2019 at 6:26 Comment(0)
M
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.

Mutualize answered 24/10, 2018 at 12:29 Comment(2)
How can you get around this if you dont want to remove the .git folders?Boxfish
@rolls see git submodulesMutualize
R
4

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

  1. https://git-scm.com/docs/git-add
Rig answered 29/7, 2020 at 19:34 Comment(0)
T
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
Transact answered 7/7, 2020 at 19:57 Comment(0)
M
-2

This worked for me

git add -f *
Mudpack answered 23/1, 2020 at 15:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.