Setting up a local git repository


If you want to use git, but don’t want to use a service such as GitHub to handle your files, you can still use it locally or on your own server.
It is surprisingly simple to do, and this article will guide step by step to setting up a working git repository on your own network or local hard drive.

This guide was written for Linux, however, same steps apply for Mac OS and Windows as well (just make sure the paths are correct for your operating system).
This article assumes that git is already installed on your machine.

Initializing an empty git repository:

We must first create a folder for the repository. You can name it whatever you want, but it should end with “.git”.
Let’s create that folder now, and move into it with the cd command:

mkdir 'My Awesome Project.git'
cd 'My Awesome Project.git'

Now, we just have to initialize it:

git init --bare
(The “--bare” argument is used to initialize an empty repository.)

Initializing the git project:

Git has to be initialized in your project folder as well. Don’t worry, it won’t mess with any of your files.
Move into your project directory:

cd '/home/me/Documents/My Awesome Project'

Initialize the git project:

git init
(This will create a .git folder in your current directory, and populate it with necessary files.)

Transferring your project to the repository:

We have created the repository and initialized our project, but we still have to make them communicate.
To tell git where your repository is located, we must add a “remote”:

git remote add origin '/path/to/My Awesome Project.git'
(For a server, the path can be an SSH address (user@IP_address:'/path/to/My Awesome Project.git') or a URL.)

Now we just have make a ‘commit’ and ‘push’ it to the repository!
If we run git status, it should list all of the files and directories inside your project. They are currently unstaged, so let’s add them to the commit:

git add . --verbose
(The dot basically means “everything in this directory”.)
(The --verbose argument is useful when adding lots of files, because it otherwise provides no output, so it may appear stuck if you are impatient. It is not required.)

We can run git status again to verify that they were added.
To commit the files, run:

git commit

A text editor will open. Write something like “Initial commit”, save, and exit the editor.

We can push the commit into the repository now. To configure git to always push to branch “master” on remote “origin” without needing to specify each time, run:

git push --set-upstream origin master

That’s all!

Congratulations, your git repository is now fully set up and ready for use. You can push, pull, clone, etc., same as you would with any other git repository such as from services like GitHub, however, you have full ownership of the repository.