How to Connect Local Folder on Your Computer to A GitHub Repository
In this post, I will show you how you can easily connect a local folder on your computer to a GitHub repository.
GitHub is a platform where code is hosted and version-controlled, and where developers can collaborate on projects.
A repository is a folder hosted on GitHub where you can store all your project files, code, and scripts. It also contains each file’s revision history.
It is good to create repositories for your projects, as not only does it help you track your project, it is also extremely helpful if there is any loss or damage to your project files, since such files can be retrieved from the repository.
Now, let us see how to create a GitHub repository and connect a local folder to it.
Create GitHub Account
If you do not already have a GitHub account, you will need to create one. Just go to www.github.com and create an account.
Create New Repository
Having created your GitHub account, the next thing to do is to create a new repository. Click on the dropdown menu shown in the image below, and click on “New repository”.
Fill in New Repository Details
After clicking on “New repository”, you will be directed to a page that looks like this
Here, you will give your repository a name and decide whether to make it a public or a private repository.
Let’s call it “example-repo”!
GitHub checks for the availability of the name you give your repository. If the name is available, then GitHub will tell you it is available as you can see in the green text under the repository name.
The next thing is to decide whether to make your repository a public one or a private one. For the sake of this tutorial, you may keep it public.
After giving your repository a name and selecting either public or private, leave everything else as-is and click “Create repository” at the bottom.
You should be directed to a page that looks like this
Great! We are now ready to link the repository to a local folder on our computer.
If you are on a Windows machine, however, you can use your good old Command Prompt.
Note here that Git is quite different from GitHub. Git is a version control system used for projects of varying sizes, while GitHub is a file hosting service. However, both systems work together.
Now, you will need to create an empty folder anywhere on your computer. I have created an empty folder called “examplefolder” in my “Documents”.
Note that you will need to know the path to your “Documents” folder in order to be able to access it within the terminal.
Here is my own path:
By running the above command, I am now inside my “Documents” folder from within my Git Bash terminal.
The next thing to do is to create a new folder or directory inside the “Documents” folder and call it “examplefolder”. Run the command below to do so:
Now when you check inside your “Documents”, you should see the newly created “examplefolder” there.
The next step is to go into that newly created folder and initialize Git inside. Here are the commands to do so:
Remember to use your own path to the newly created folder or directory.
After running the above commands, you should see a message like this
Initialized empty Git repository in C:/Users/IFEANYI/Documents/examplefolder/.git/
From this point on, Git is monitoring any activities in that folder.
Connect Folder to GitHub Repository
Now, let us go back to our GitHub repository and copy some commands which we will run in our terminal.
The first command will convert our repository branch from “master” to “main”. This is how GitHub now recognizes the main branch of any repository created.
Copy and run the command below:
git branch -M main
If all goes well, you will notice that the “examplefolder” now has “main” in brackets in front of it.
Next, we will connect our local folder to the GitHub repository we had created earlier.
Copy and run the command below:
git remote add origin https://github.com/Ifeanyi55/example-repo.git
The local folder has now been connected to the GitHub repository.
Remember to substitute “Ifeanyi55” with your own GitHub account name before running the above command.
Now, we will create a text file inside the repository folder on our computer. Run the command below:
echo This is an example repository > examplerepo.txt
Let us now check if the text file has been created inside our repository folder. Run the next Git command:
You should see the text file name in red as displayed below:
That means it does exist inside our repository folder. It is currently in red because it has not been added to the staging area. Let’s now do that!
Run the next command:
git add .
Take note of the dot after “add”. That signifies adding any file in the repository folder to the staging area.
After that, run “git status” again, and you will see that the file name is now in green color, which means it is ready to be committed.
Commit and Push
The last two commands to be run will commit the file and push it to GitHub. Let’s run the commit command and give it a message:
git commit -m "New commit"
The commit message will appear in front of the file on GitHub, and it is a good way to help a developer keep track of any changes they make in any file in the repository.
The final step is to push the file to GitHub. Run the command below:
git push -u origin main
You should see an output like the one in the image below in your terminal
Nice! Lastly, go back to your GitHub repository and refresh the browser. You will then see your fully functional repository containing your text file.
Congratulations! You have successfully connected a local folder on your computer to a GitHub repository.
You can also turn any existing folder on your computer into a repository folder following the same steps outlined in this post.
Going forward, if you add any file to that local folder, you can send it to the GitHub repository.
I hope you found this post helpful!
Follow me on Medium: @ifeanyidiaye
Follow me on Twitter: @Ifeanyidiaye