Main Content

How to add GIT to a project with existing files.

Here is the code I use to add GIT to an existing project.

git init
git remote add origin GITURL
touch .gitignore (optional see below)
git add -A
git commit -m 'first commit'
git push

If you try to clone a GIT repository to a folder with existing files, GIT will be unhappy as the folder isn’t empty. However, the code above works for me without having to move any files.

I’m no GIT expert, so if there is a better way of doing this, please do share.

I’ve updated the code to create a GIT ignore file. This is a good idea as there are files you may not want in your repo. Things like .htaccess files, config files, or maybe some temporary or cache files. If you choose to create the .gitignore file, don’t forget to add all the files you want to exclude before doing git add -A.

Leave a Reply