Convert a folder to a Git repository
I recently created a folder and started working on a Azure ARM Template that I then wanted to push into a GitHub repository so I could share it with others. So I had to convert a non-empty folder into a Git repository.
There are a few processes to carry out in order to make it happen. 😊
Create a new repository
Create your repository source, in my example I am creating a new GitHub repository. With my new repository create I take a note of the URL, as you'll need it in a few steps.
Convert the folder
The next step is to convert the folder, to do this open up a command prompt or Terminal shell (I'm using Windows Terminal).
Navigate to the location of the folder and type in the command:
git init
This command will create a folder called .git and is usually the first command you run on an empty or non-empty folder to covert to a Git repository.
Add files
You now need to add the files you've created into the "staging" area to get ready to push them to your repository source.
git add .
Commit your files
The next step is to record the changes to your repository.
git commit -m "add my files"
Configure the remote repository
Your next step is to configure it so that your local folder/repository knows where the remote repository is. This is the one you created earlier on at the start of this process.
git remote add origin <url>
Push the files to the remote repository
The next step is to push a copy of the files from your local repository to your remote repository. Within this stage you also need to configure the branch you are using and the name of it. You can do this using the following command.
git push -u origin main
You've now successfully created your local folder into a Git folder and configured the remote repository. Any changes you make locally can now be easily pushed to your remote repository.