This is a clear and simple step-by-step tutorial showing how to set up a git repository locally and on a remote server. You will need this for sharing your work with other people and/or machines.
Creating a new git repository on your local machine is a very simple operation which gives you a full working directory. Basically a git working directory is a local repository which tracks revisions of your files and a history of your commits.
In case you wanted to share your files with other people or access them from other machines, all you need to do is to create a bare repository on a server and push your changes there. A bare git repository is a special repository without a working directory. When you create a bare git repository it only contains git objects and it will only contain git objects even after you start pushing content to it. Git objects are files used by git to keep track of all your data and its multiple revisions.
For this tutorial I am assuming you will be setting things up on a Linux server, but the process is pretty much the same on other operating systems.
How to set up a git repository locally
The first thing you need to do to set up a git repository locally is creating a new directory and move to it
$ mkdir YourProjectName $ cd YourProjectName
Now you’re inside your project directory and you can create a git repository with the following command:
$ git init Initialised empty Git repository in /path/to/YourProjectName/.git/
Listing the content of the directory will show you something like this:
$ ls -lha total 12K drwxrwxr-x 3 m3xican m3xican 4.0K Dec 4 00:45 . drwxr-xr-x 15 m3xican m3xican 4.0K Dec 4 00:42 .. drwxrwxr-x 8 m3xican m3xican 4.0K Dec 4 00:42 .git
As you can see git created a hidden directory which it uses to keep all the files it needs to track your work.
Now your local git repository is ready to use and you can work in it and commit your changes following a workflow similar to this:
...create and edit *FILES*... $ git add *FILES* $ git commit -m "Commit message describing things you did."
Once you have finished on your local machine you can move to the server.
How to set up a git repository on a remote server
Now that you are on the server you have to reproduce the same steps from the previous section for creating a git repository. There will be few minor differences, but core concepts are pretty much the same.
The first thing you want to do is to create a directory to store the bare git repository. I usually keep all my repositories in /var/repo/git/, but you can use other paths like /home/git/ or anything you prefer.
$ mkdir YourProjectName.git $ cd YourProjectName.git
This time the directory ends with .git because we are creating the bare repository and not a working directory.
Now let’s initialise a bare git repository from the newly created directory with the following command:
$ git init --bare Initialised empty Git repository in /path/to/YourProjectName.git/
Listing the files in the directory will give different results this time:
$ ls -lha total 40K drwxrwxr-x 7 root root 4.0K Dec 4 11:39 . drwxr-xr-x 17 git git 4.0K Dec 4 11:39 .. drwxrwxr-x 2 root root 4.0K Dec 4 11:39 branches -rw-rw-r-- 1 root root 66 Dec 4 11:39 config -rw-rw-r-- 1 root root 73 Dec 4 11:39 description -rw-rw-r-- 1 root root 23 Dec 4 11:39 HEAD drwxrwxr-x 2 root root 4.0K Dec 4 11:39 hooks drwxrwxr-x 2 root root 4.0K Dec 4 11:39 info drwxrwxr-x 4 root root 4.0K Dec 4 11:39 objects drwxrwxr-x 4 root root 4.0K Dec 4 11:39 refs
It might be interesting to know that these are the same files you will find in the .git directory created by git in your local working directory.
In my case both the new directory and all the files in it are owned by the user root. That means that git can’t write them and we need to fix that before continuing:
chown -R git .
chgrp -R git .
Now the content of the bare repository should look like this:
$ ls -lha total 40K drwxrwxr-x 7 git git 4.0K Dec 4 11:39 . drwxr-xr-x 17 git git 4.0K Dec 4 11:39 .. drwxrwxr-x 2 git git 4.0K Dec 4 11:39 branches -rw-rw-r-- 1 git git 66 Dec 4 11:39 config -rw-rw-r-- 1 git git 73 Dec 4 11:39 description -rw-rw-r-- 1 git git 23 Dec 4 11:39 HEAD drwxrwxr-x 2 git git 4.0K Dec 4 11:39 hooks drwxrwxr-x 2 git git 4.0K Dec 4 11:39 info drwxrwxr-x 4 git git 4.0K Dec 4 11:39 objects drwxrwxr-x 4 git git 4.0K Dec 4 11:39 refs
This means that now git has full access to all the files contained in the bare repository directory.
Connecting a local git repository to a remote server
Once the working directory and the bare repository are set up it’s time to connect them to save your work on the server and eventually share it with others. This is done by the following command:
$ git remote add origin [email protected]:/path/to/YourProjectName.git
This command doesn’t show any output message on success, but you can verify everything is fine visualising the tracked repositories:
$ git remote -v origin [email protected]:/path/to/YourProjectName.git (fetch) origin [email protected]:/path/to/YourProjectName.git (push)
Now your local working directory is connected to your remote bare repository and you can push changes to it.
Pushing local changes to the server
Now that the local working directory and the bare repository on the remote server are connected you can push changes to the latter.
You might be tempted to try a simple push, but that will give you an error:
$ git push fatal: The current branch master has no upstream branch. To push the current branch and set the remote as upstream, use
That’s because you need to specify an upstream branch (the one used by default when no branch is specified) and associate it with a remote one. You can do that with option –set-upstream or its shorter version -u as showed next:
$ git push -u origin master
This will also push all the changes in the local branch master to the remote server origin.
From now on git will keep pushing changes from master to origin every time you use the git push command.
You might be interested in reading more about the git commands mentioned in this tutorial:
I hope you enjoyed this tutorial explaining how to set up a git repository locally and on a remote server. If you have any question feel free to leave a comment.
If you found it useful, please share it on social media using the social buttons below.
Don’t forget to subscribe to the blog newsletter to get notified of future posts (once a week).