summaryrefslogtreecommitdiff
path: root/git.html
blob: b5dd616db759d9a3821124cef24a52f51d3b4123 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<!DOCTYPE html>
<html lang=en>
    <head>
        <title>Hosting Your Own Git Repositories &ndash; LandChad.net</title>
        <meta charset="utf-8"/>
        <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
        <link rel='stylesheet' type='text/css' href='style.css'>
        <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel='alternate' type='application/rss+xml' title='Land Chad RSS' href='/rss.xml'>
    </head>
<body>
    <header><h1>Hosting Your Own Git Repositories</h1></header>
    <nav></nav>
    <main>
        <img class=titleimg src="pix/git.svg">
        <p>
        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</p>

        <pre><code>git clone github.com/...</code></pre>

        <p>to</p>

        <pre><code>git clone YourLandChadDomainName.xyz/...</code></pre>

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

        <h2>Installing git</h2>

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

        <pre><code>apt install git</code></pre>

        <p>
        We don't need any additional software, <code>git</code> itself ships
        with everything needed to host a remote repository!
        </p>

        <h2>Creating bare repositories</h2>

        <p>
        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.
        </p>

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

        <h3>Become the git user and create the directory</h3>

        <p>
        If you're logged in to your server as root and have <code>git</code>
        installed, you can become the <code>git</code> user by executing
        </p>

        <pre><code>su git</code></pre>

        <p>
        Now navigate to/create your desired directory, for example
        </p>

        <pre><code>cd /srv
mkdir git</code></pre>

        <h3>Create the repo</h3>

        <p>
        Now you can create the bare repository with
        </p>


        <pre><code>git init --bare my-repo.git</code></pre>

        <p>
        By convention, bare repository names end with ".git".
        </p>

        <p>
        Repeat the above command for any other repositories you want to host.
        </p>

        <h2>Syncing local repositories with your server</h2>

        <h3>Set up SSH login for the git user</h3>

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

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

        <p>
        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 <code>user@host:path</code>, where:
        </p>

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

        <p>
        So, to create a new remote, run:
        </p>

        <pre><code>git remote add origin git@yourdomain.xyz:/srv/git/my-repo.git</code></pre>

        <p>
        Now you'll be able to run <code>git push origin master</code> to push
        your commits or <code>git pull origin</code> to pull from the remote.
        </p>

        <h3>Syncing an existing repository</h3>

        <p>
        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:
        </p>

        <pre><code>git remote -v</code></pre>

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

        <pre><code>git remote add vps git@...</code></pre>

        <p>
        Now you'll be able to push/pull with <code>git push vps master</code>
        and <code>git pull vps</code>, respectively.
        </p>

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

        <h2>Contribution</h2>
        <ul>
            <li>Martin Chrzanowski -- <a
                href="https://m-chrzan.xyz">website</a>, <a href="https://m-chrzan.xyz/crypto.html">donate</a></li>
        </ul>
    </main>
	<footer><a href="https://landchad.net">LandChad.net</a></br>Because Everyone should be an Internet LandChad.</br><li><a href="index.html"><img src="pix/chad.gif" alt="chad"></a></li><li><a href="rss.xml"><img src="pix/rss.svg" alt="RSS"></a></li><li><a href="pix/btc.png"><img src="pix/btc.svg" alt="BTC"></a></li><li><a href="pix/xmr.png"><img src="pix/xmr.svg" alt="XMR"></a></li><li><a href="https://github.com/lukesmithxyz/landchad"><img src="pix/git.svg" alt="Github"></a></footer>
</body>
</html>