3

When adding a folder with some new files into my project and doing a git status, git only tells me about the folder and not the files. When I did a add -A only the folder and no files was added and when I push and pull, only the folder and not the files come through.

What am I doing wrong?

Johan Dahl
  • 1,672
  • 3
  • 19
  • 35

5 Answers5

7

Let's say you have the following untracked folder:

./fruits/
./fruits/apples.txt
./fruits/kiwis.txt

If you want to see the files inside the untracked folder:

git status -u

If you want to stage the untracked folder, together with the files inside it:

git add fruits/
airimiea
  • 83
  • 1
  • 5
2

As git only tracks files and not folders, the behavior regarding push and pull is impossible. It is impossible to add an empty directory to git.
For git status however this is expected behavior as all files in the folder are new, because git doesn't track any file inside this folder yet.

Also, check the git school, challenge 7. If you type git status there, you will see this output, although octofamily contains two files:

$ git statu­s
# On branch master
# Untracked files:
#   (use "­git add <file>..." to­ include i­n what wil­l be commi­tted)
#
# blue_octocat.txt
# octofamily/
# red_octocat.txt

This is just a simplification in the UI, you still can do git add octofamily/baby_octocat.txt (However, not in the git school, because it isn't an actual git client).

A subsequent git status would now reflect this:

$ git statu­s
# On branch master
# Changes to be committed:
#      new file: octofamily/baby_octocat.txt
#         
# Untracked files:
#   (use "­git add <file>..." to­ include i­n what wil­l be commi­tted)
#
# blue_octocat.txt
# octofamily/momma_octocat.txt
# red_octocat.txt

Please note, how it now shows the file(s) inside octofamily, because now that folder is known.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • That is very odd because I got an empty folder on my site when I pushed and pulled to it. However, how do I start tracking the files in the folder then? – Johan Dahl Jan 17 '13 at 15:20
  • @JohanDahl: Please see update. The empty folder indeed is strange. – Daniel Hilgarth Jan 17 '13 at 15:22
  • Ok then there was nothing wrong with my git status I guess. But I did do git add . and also tried git add -A and the files where not added, atleast git status didn't report anything about them. I finally managed to get them recognized by going inside the folder and doing git add . from there. But shouldn't that work from my root folder where the git project is? – Johan Dahl Jan 17 '13 at 15:46
  • @JohanDahl: Yes, that should work from the root folder, I just checked it on my local repository. I have to admit, your problems sound a bit odd. Maybe it is a good idea to post that on the git mailing list. – Daniel Hilgarth Jan 17 '13 at 16:06
  • The behavior described is what I see when I add a submodule, the add for the files in the submodule need to be added within the submodules folder, but only the folder itself is visible from outside the submodule. – David Culp Jan 17 '13 at 18:29
  • @DavidCulp: Yes that is exactly my issue. Anyway, even though the behaviour is still suspicious, atleast I now know how to bypass it. Thanks. – Johan Dahl Jan 18 '13 at 15:45
  • Just to clarify -- you are using submodules? – David Culp Jan 18 '13 at 17:30
  • @DavidCulp: No, actually I'm pretty new to Git and wasn't aware of what submodules is or how they work until I just googled it. My problem did arise however when I cloned a drupal module into my project. So I did have two git-repositorys, my main repository and then the module that I cloned. I thought they could work independently by themselves but I guess that's what submodules are for. The problem persisted for me though even when removing the drupal module and downloading it without the git-part. – Johan Dahl Jan 21 '13 at 08:22
2

Note that git will now treat folders containing a hidden .git folder as a submodule. You can check them in that way, leaving the folder contents accessible as a separate repository with only a reference stored in the parent repository.

1) You can keep it that way and just use the two repos separately if you prefer, linked by a reference.

2) If retaining the git history of the folders contents is not important, deleting the .git folder inside will cause the normal git behavior to return.

3) If you want to retain the history, you can instead use git subtree to copy a branch into your new repo. This makes the files editable locally and the history is still accessible.


The following command will copy the contents and history of another local repo's current branch into your current local branch.

git subtree add --prefix=[newFolderName] [/path/to/other/repo] HEAD

( Replace the square bracketed sections with the name and path of your choice )

eadsjr
  • 681
  • 5
  • 20
0

Try git add one of existing files in the directory. git status and it will show all files there as usual. This worked for me.

-1

In my case, there was .git directory folder hiding in the subdirectory. removing it fixed the issue for me.

user3152459
  • 355
  • 4
  • 12