git sub directory

how to add subdirectories in git

Adding subdirectories in existing git repository

You have done git push but the new folders didn’t got pushed. What happened?

Having new folders and subfolders in the git repository, but the files under those not showing in the server?

Adding subdirectories in existing git repositories should have been direct right? But, sometimes it is not. I will show you How to add subdirectory in existing git repository

Let’s say you have existing git repository my-git-repo and you want to add new folder under it: new-folder/another-sub-folder

Step by steps to add git subdirectories in existing git repository

1. Create folder in the repository you are going to create new folders

mkdir -p new-folder/another-sub-folder

2. Now if you do git status you will see the new folder under new listing.

3. Create a file under another-sub-folder otherwise git won’t show nothing.

cd new-folder/another-sub-folder && touch some-file.xml

4. Go to another-sub-folder and issue git init

5. Now go back to the root of the folder and issue git add new-folder/

6. You are done, when you do git status you will see your new files being listed for commit.

You asked to pull from the remote but did not specify the branch github error

asked to pull from remote but did not specify the branch github error

Mostly, you got a new repository or even existing one and you are trying to interact with it.

The above problem would be happening this time.

Git would allow you to have many branches of the same repository. So when you try to interact with the repository and you didn’t specify which branch then this would be the problem.

Because this is not the default configured remote
for your current branch, you must specify a branch on the command line

The tasks you are trying to do might be

git pull
git push or even others.

Solution

The first thing you want to do would be to check what branch you are in.

git branch

If you see master in there * master then do

git pull origin master

This would take care of the problem

Git add not working for folder with .git folder

Also you might get something like
fatal: Pathspec ‘some.file.name.goes.here’ is in submodule ‘some/folder/name’
Also you might notice, if you commit and pushed, only some skeleton of the of the folder structure without the actual files would be committed.

The root of the problem is the folder you are trying to add is tracked under git itself.
This would confuse git and would rather leave it up to you.

the fix
First go ahead and remove the .git folder in the new directory

sudo rm -rf /path/to/new/folder/.git

Then clear the cache from git memory of the new folder

git rm -r --cache /path/to/new/folder

Then when you do the

git status

you will see all the new files being listed and you can commit that.

git file

Git diff with filemode changes

When you do a

git diff

And you see a lot of files you haven’t changed you would say ‘what is going on’?? and bang your head with brick wall – well don’t do that please :)

Mostly it might have something todo with the file changes taking place. Especially if you have moved your folder or something like that, it would be the problem.

Here is the good news, you can tell git not to worry about the file mode changes through

git config core.fileMode false

This should take care of the problem you are having.

That would take care of git showing all the files being changed to their normal state.