I recently created a folder and started working on an Azure ARM template that I wanted to push into a GitHub repository to share with others. And I had to convert my folder with contents into a Git repository.
There are a few steps to carry out 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 created, I note 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:
This command will create a folder called .git and is usually the first command you run on an empty or non-empty folder to convert to a Git repository.
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 configuring it, so your local folder/repository knows where the remote repository is. This is the one you created earlier 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 its name of it. You can do this using the following command.
git push -u origin main
You've successfully created your local folder into a Git folder and configured the remote repository. Any local changes can now be easily pushed to your remote repository.