I need git to include the parent directory

1.5k Views Asked by At

I have a program divided into 3 packages. I have set up a local repo in one of the packages and set it up to push to a repo on gitHub. I have started work on the second package and I realized I haven't initialized it with git. I wanted to use the same gitHub repo to push the second package to but I'm not sure it will work since I have already set up my first package to push to it. I think I should have done git init on the parent folder of the three packages and then the folder/package structure would have shown up on my gitHub repo. Is there a simple way to fix this? Thanks!

2

There are 2 best solutions below

2
On BEST ANSWER

Let's say your parent directory is named parent, and your child directories are named package1, package2, and package3. Let's also assume that package1 is the directory where you have your git repository. I would just make another package1 directory as a child of the original package1 directory, and move the contents of the original package1 directory into it. Then move package2 and package3 to be children of the original package1. You can the rename the original package1 to parent. git add --all . the whole thing, and you now have a repo with all three directories. The cool thing is that you will not lose the history of all the files you moved; git is smart enough to detect renames/moves.

1
On

Assume we have directory structure like this:

    A
    |
    B
    |
    .git and other files

You should:

cd B
mkdir C
ls | grep -v '^C$' | xargs -L 1 -I '{}' git mv -r '{}' C/
ls ../ | grep -v '^B$' | xargs -L 1 -I '{}' mv ../'{}' .
git commit -am 'Change directory structure and include parent dir'