Convert a folder to a Git repository
Converting a standard folder into a Git repository is a fundamental step in version control and collaborative software development. This blog post serves as your guide to the essential process, empowering you to seamlessly integrate version control into your projects, whether you're a seasoned engineer or just starting your coding journey.
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:
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 convert 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 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.