Erik Sundberg
Erik Sundberg

Reputation: 91

How do i create a Git Repo from multiple directories

I am working on a asterisk project and would like to put the code in Git for version tracking.

Here are the 3 directories that the code is in:

I don't want to track these as 3 separate repo's in Git. How would I put it all in one Repo?

My Google fu is not coming up with anything relevant.

Upvotes: 6

Views: 4832

Answers (3)

Saurabh P Bhandari
Saurabh P Bhandari

Reputation: 6742

As mentioned in other answers, you could move the directories under a common parent directory and track it as one repository or use the sub-modules approach.

In case you don't want to move the existing directories, you could treat the root directory / as your parent directory (provided you have the necessary permissions).

# Initialize git repo at root
git init /

# Add the directories to be tracked
git add /path/to/directory1 /path/to/directory2

However, this causes a lot of files to be untracked (see git status output) since the repository is initialized at the root directory. One way to ignore all these files is to add them to $GIT_DIR/info/exclude file.

Open an editor to modify this file /.git/info/exclude to :

# ignores everything under root directory
/*

# except these paths
!/path/to/directory1/*
!/path/to/directory2/*

You can confirm with git status that only the directories you added previously have been staged for the new commit and it should no longer show any untracked files.

If satisfied, create your first commit

git commit -m "Your commit message"

Upvotes: 1

chelmertz
chelmertz

Reputation: 20601

You could move all those folders into a newly created directory, where you start a git repository and add all files, then you symlink the files back again to their original paths.

Something like this (please backup your files first, in case you (or more likely, I) made a mistake):

mkdir -p ~/repos/my-asterisk
mv /etc/asterisk/custom /var/lib/asterisk/agi-bin /var/www/html/  ~/repos/my-asterisk
cd ~/repos/my-asterisk
git init .
git add .
git commit -m "init"
ln -s $PWD/custom /etc/asterisk/custom
ln -s $PWD/agi-bin /var/lib/asterisk/agi-bin
ln -s $PWD/html /var/www/html

This has some downsides, for example:

  • the user/group/permissions of the files in the git repo have to match 1:1 against what your system should look like (i.e. using the git repos' files on another server may require the exact same setup)
  • when you upgrade system packages/your distribution/anything else that touches those directories, they will record changes in your git repo. This may or may not be what you want. You will have to keep track of when to commit those changes (i.e. git status periodically in this repository).

This may be fine if you only use this on your single computer, and storing things in git is just a quick safety measure.


You could skim:

and when you want to spend serious amount of time for this, you will end up reading about configuration management systems :)

Upvotes: 0

Michael Hayashi
Michael Hayashi

Reputation: 88

This may not be what you're looking for, but you can consider:

  1. Adding git submodules or...
  2. Moving directories under same parent git directory

Go into an initialized git repo where you would want to store the other repos. Then, inside the same directory, use:

git submodule add <repo url>

For example,

git submodule add https://github.com/rust-lang/rust.git

You can find the repo url by going to the repo's main page and clicking "clone or download," then copy the link.

If these are not already git repos, then you cannot add these directories to the same git repo. Every directory you include needs to be a subdirectory of the git initialized directory. If they were, then you can use:

git add <directory path>

For example,

git add ./subdirectory1/subdirectory2

Upvotes: 0

Related Questions