Add existing source code folders into Local Git Repository
Question
Let's say I have two existing folders that I want to put under Git
source control. One is called CurrentProduction
and the other CurrentDevelopment
. Would the following steps be correct?
- Create a new folder called
MyProject
git init
- Move the
CurrentProduction
contents toMyProject
git add .
git commit -m 'initial master commit'
git checkout -b develop
- Delete all files in
MyProject
(except for the .git folder of course) - Move the
CurrentDevelopment
contents toMyProject
git add -A
git commit -m 'initial develop commit'
Or is there a "different" way?
Solution
Doing it the way you describe will not do any harm.
You can move the
.git
directory into the tree you want to commit instead of moving the tree to the repository.You can use the
--work-tree
option orGIT_WORK_TREE
environment variable to act as if you did that:git add --work-tree=CurrentProduction -A; git commit ...
Note that it is not necessary to specify it for commands after
add
sinceadd
copies content into the index,commit
uses the index, and the index is in.git
.
As a separate issue, note that your procedure will make the develop
initial commit a child of the master
initial commit. That is probably the right thing for this case; I just want to make sure you're aware it's not symmetric.
OTHER TIPS
It is a good way.
The only possible issue if you look at the history of the develop branch with git log
you will have the current production project state as last commit (the one with 'initial master commit'). Which semantically not fully correct.
If that bothers you can make first an initial empty commit which servers as a common base for the two branches:
git init .
git commit --allow-empty -m "initial commit"
# add CurrentProduction content
git add .
git commit -m "initial master commit"
# checkout at the empty commit
git checkout -b develop HEAD^
# no need to delete any files!
# working tree is empty
# add CurrentDevelopment content
git add .
git commit -m "initial develop commit"