How to create and use a local Git repository

Radu Zaharia
3 min readMay 9, 2022

--

Photo by Yancy Min on Unsplash

I touched a bit on this back when I was writing my “how to get rid of cloud services” series. I know, these days everything happens in the cloud and you probably use Github or Gitlab or other spinoffs and it’s easy and convenient enough.

But have you ever wondered how to create a local Git repository? Maybe to store your code locally or in a home network on a NAS, or to share it only within your private VPN, or to have full control over it: there are plenty of reasons, security or otherwise and in this article we will see how to do it.

Creating an empty Git repository

Photo by Zach Reiner on Unsplash

When we want to create a folder handled by Git we simply use the git init command. When we want to create a Git repository, we use the same but we add the --bare parameter. This command will simply create Git specific files and folders at the current path.

#mkdir repo
#cd repo
#git init --bare

That’s all for the repository. Again, if you simply run git init, you will have a Git controlled folder but it will not act as a repository. You cannot clone it. The resulting file and folder structure is also not the same. Here’s what’s in the repo folder after running git init --bare:

The repository files and folders

There’s a lot of stuff going on there. Here is in comparison another folder called clone where I ran git init without parameters:

The clone files and folders

Now you can simply clone the repository anywhere using git clone and start adding code to it. But what if you decided to create your local Git repository after already writing a bunch of code? You could clone the repository and copy your code there, or you can track the repository manually. Let’s see how:

The first commit

Photo by Robert Katzki on Unsplash

We need to add stuff to our newly created Git repository by pushing our first commit. So let’s go to our clone folder, add a readme file to it and set the Git origin to our repository at the repo folder:

#cd clone
#touch readme
#git remote add origin ~/Projects/repo

We are setting the current Git folder’s remote origin to our repo folder created in our home, in Projects. You can use a better name and it should usually end with .git, like repo.git. But for our exercise I just used repo. Now let’s add the readme file to a commit and push it to the Git repository:

#git add readme
#git commit -am "Added a readme file"
#git push --set-upstream origin master

Note that the first commit after setting the remote origin needs the --set-upstream parameter. We need to set the upstream branch before we can push to our repository. And the upstream branch is the only one we currently have, master. Our commit reached the repository and everything is ready.

This was a shorter article just to showcase how to create a fully local Git repository and how to track it from a current folder where you already wrote some code.

Creating local Git repositories allows you to start your own Git “server” on your home network NAS and share code with your local network or your VPN members. Feel free to comment and to add suggestions. I hope you enjoyed the article and see you in the next one!

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response