Sometimes I play around with some idea and after a while I decide I actually want to keep it instead of just abandoning it. So I should save it in a git repo, but I never remember the commands to send my existing codebase to a new empty repo. For future reference, here they are
Run these commands from the root of your source tree. Make sure that you delete anything you don't want to commit first, such as backup files and compiled code. Alternatively you can add all files separately by using git add
without the -A option. I have a few git repos stored in a directory on my local filesystem which is what I will be using, but the commands aren't much different if you want to push to gitlab or github - you just need to set up SSH access first and initialize the repo through their web interface.
# create empty repo in the destination directory
git init --bare /path/to/repos/project.git
# init git repo in the project directory
git init
# add everything (make sure you don't have anything you don't want in the source tree)
git add -A
# commit it to project repo
git commit -m "Initial commit"
# add remote called origin (in my case it's on the local filesystem, but still considered remote)
git remote add origin /path/to/repos/project.git
# force push to remote (make sure you don't have anything you want to keep on the remote)
git push -u -f origin master
If you want to use a repo on another server using SSH, then replace the git remote
line with something like:
# add remote called origin (accessed via SSH)
git remote add origin user@server.org:/path/to/repos/project.git
If you find that you did commit files you didn't want to, then you will need to use git rm to remove them and commit and push the changes.