In this tutorial you will learn how to how to quickly set up a simple, low-requirements, no-database, no-webserver Git repository. All you need is (unprivileged) SSH access and the git program itself.
When you start your own project or startup, you’ll want a (remote) Git repository for yourself or other collaborators. This is especially true when the project is something you want to keep private, i.e. the next big thing.
Maybe your project is so great that you’re paranoid about it being stolen. Or maybe it’s not even code that you want to store, but a personal log or text that you want to have remotely backed up.
There are cloud services which provide Git repositories for this purpose, but most of them are paid. Since you already have your own server, why use a third party?
Sure, there are self-hosted solutions, like Gogs, GitLab and others, but those might be overkill. You don’t want to run and maintain a web server and a database, while doing complex user management…all just to host a Git repository for a small project.
Here, I’ll show you how you can set up a Git repository for yourself. You only need an SSH account and a working install of the Git program (the git command itself). If git is installed you won’t even need root access.
- SSH account on your remote system
- Git installed
Depending on whether you have root access and whether you want to share the repository with others, you might want to consider creating a specific user for your shared git service. This is completely optional and not required at all.
We will use an SSH account for authentication, so if you share the git repository, the person you share it with will have the same permissions as that user. It might make sense to create a separate user (maybe called git), if you worry about collaborator having access to this account.
We won’t cover how to create an account here, so if you want to do that, simply create a new account and use that one.
Creating the git repository
Connect to the server, using the account you want your repository to belong to (eg. git or your own user)
Check if git is already installed
This should output the version of git that is installed on your server. If you receive a message like “command not found” then git is not installed on your system. Simply run “apt-get install git” or whatever the package installation command is on your system. Just check the documentation of your operating system/Linux distribution.
Now you can initialize a new bare repository. In this case we will call it “my-project”. You can create this wherever you want. For the sake of simplicity we will assume that it is your home directory.
git init --bare my-project.git
This will create a directory called my-project.git. It is not a file, but a directory structure that git considers a repository. We won’t go into detail here and it will probably be quite a while until you need to change something.
Believe it or not, that’s actually all you need to do!
You can now disconnect from the server.
Using the repository: The initial push
Despite it being completely empty, you can already clone the repository:
git clone email@example.com:my-project.git
As mentioned earlier, this assumes that you created the repository in the home directory of the user. Starting after example.com, the path is relative to the user’s home directory. If you want to specify a full (absolute) path, just start with a slash. So using firstname.lastname@example.org:/home/myuser/my-project.git would lead to the same directory being cloned.
Git will warn you that you cloned an empty repository. But since this is what we expect, there’s no need to worry.
We can now switch to the cloned directory and start working on the project.
cd my-project echo "My personal project" >> README git add README git commit -m 'initial commit'
Now that we have our first commit, we can push it. The very first push has a minor caveat to be aware of: because the repository is still completely empty it doesn’t know any branches yet, not even the master branch. Git will tell you this if you only run git push. So just make sure to tell git the branch when pushing to a new repository for the very first time.
git push origin master
Now you or someone else with access to your SSH account can clone, push and pull from and to this repository. You can even set up hooks and do other fun things. Git is a very powerful tool with a huge variety of features. It can take a while to get used to them, so just consider this the start. However you are now equipped with a very simple and low-requirements git setup.
I hope you enjoyed this tutorial. If you have any questions, don’t hesitate to write a comment.
I’d also love to hear where you went from here. Did you switch to Gogs, GitLab or Gitolite? Or did you maybe extend it with fancy hooks and scripts?
Consider sharing what you’re using such a lightweight setup for. Is it your coding projects, or do you use it as a diary, notes file, or something else? Maybe you back up config files or other data in there?
The possibilities are endless, and I’m always excited to hear about people using Git for interesting or unique use cases.