summaryrefslogtreecommitdiff
path: root/content/git.md
blob: f7cb6b53c9d8857ef45f381367f92e82e74cece4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
---
title: "Git Server"
date: 2020-07-01
icon: 'git.svg'
tags: ['service']
short_desc: "Hosting your own basic git server."
---

Once you have your own VPS or other Internet-available server, you can
start hosting your own git repositories. The goal of this tutorial is
for you to go from

```sh
git clone github.com/...
```

to

```sh
git clone YourLandChadDomainName.xyz/...
```

so you can cultivate your own homegrown, grass-fed code, rather than
relying on a centralized proprietary service like GitHub.

## Installing git

You most likely already have it installed on your server, but if not,
run:

```sh
apt install git
```

We don\'t need any additional software, `git` itself ships with
everything needed to host a remote repository!

## Creating bare repositories

For each repository you want to host, you will need to manually create
what\'s called a \"bare\" repository on your server. These hold all the
commits and any other git data needed for your repository, but without
an expanded \"index\" in which you can just browse all the files of a
certain commit in the file system.

These repositories need to be owned by the `git` user, and you should
probably pick a directory where you will store them all. One sane choice
is under `/srv/git/`, and we will use this as the example directory for
the rest of the tutorial, but any other path will do as well.

### Become the git user and create the directory

If you\'re logged in to your server as root and have `git` installed,
you can become the `git` user by executing

```sh
su git
```

Now navigate to/create your desired directory, for example

```sh
cd /srv
mkdir git
```

### Create the repo

Now you can create the bare repository with

```sh
git init --bare my-repo.git
```

By convention, bare repository names end with \".git\".

Repeat the above command for any other repositories you want to host.

## Syncing local repositories with your server

### Set up SSH login for the git user

You will need to be able to login remotely via `ssh` as the `git` user
we\'ve used before. To do this, you will either need to set up a
password for the `git` user by running `passwd git`, or copy your public
SSH key from your local machine to `/home/git/.ssh/authorized_keys`. See
the [SSH keys instructional](/sshkeys) for details (just log in as
`git` instead of `root`).

### Syncing a new repository with your server

If you\'ve just created a new repository on your local machine, you will
need to tell `git` where the remote repository is to be able to sync
with it (using commands like `git push` or `git pull`). We do this by
defining a \"remote\" for your repository.

A remote is just a named URL remembered in your repo\'s configuration.
So we need a name and a URL. By convention, the \"main\" remote is
called \"origin\". The URL has the format `user@host:path`, where:

-   `user` is `git`, the `git` user we\'ve already worked with before.
-   `host` is your domain name. Alternatively you could even use your
    server\'s raw IP address.
-   `path` is the absolute path to the repository on the server, in our
    example `/srv/git/my-repo.git`

So, to create a new remote, run:

```sh
git remote add origin git@yourdomain.xyz:/srv/git/my-repo.git
```

Now you\'ll be able to run `git push origin master` to push your commits
or `git pull origin` to pull from the remote.

### Syncing an existing repository

If you\'ve already set up your local repository to sync with a service
like GitHub it probably already has a remote called \"origin\". You can
see your repo\'s remotes with:

```sh
git remote -v
```

You can follow the above instructions, substituting an arbitrary other
name other than \"origin\" to create a differently named remote, e.g.

```sh
git remote add vps git@...
```

Now you\'ll be able to push/pull with `git push vps master` and
`git pull vps`, respectively.

Or, to completely sever ties with your centralized git provider, first
remove the original origin with: `git remote remove origin` and then
follow the instructions as above.

## Contribution

-   Martin Chrzanowski \-- [website](https://m-chrzan.xyz),
    [donate](https://m-chrzan.xyz/donate.html)