How do i create a Git Repo from multiple directories
Asked Answered
M

3

6

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:

  • Asterisk Dialplans: /etc/asterisk/custom
  • Asterisk AGI Code: /var/lib/asterisk/agi-bin
  • Web Interface: /var/www/html/

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.

Mind answered 29/4, 2020 at 21:41 Comment(3)
Out of curiosity, can you update on how you solved it ?Bio
Does this answer your question? Is it possible to add and commit files outside of a git repository?Belligerent
Some alternatives are outlined in #2384254 as wellBelligerent
B
1

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"
Bio answered 29/4, 2020 at 23:27 Comment(0)
I
0

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
Imperative answered 29/4, 2020 at 21:46 Comment(0)
D
0

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 :)

Deloisedelong answered 29/4, 2020 at 22:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.