I decided to blog this on as I found that the general articles seems to be missing a few steps, which makes getting a new server repo working properly. So this guide is a reminder to me (primarily).
At the server (which we call server01.sr)
# sudo adduser git
# su - git
$ cd /home/git
$ mkdir .ssh && chmod 700 .ssh
$ touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys
You’ll need to have your own ssh keys in order to login to the server. To create pair of keys (a pair consist of a private key and a public key. The command below will create a keypair using RSA as the default algorithm.
$ ssh-keygen
But if you’re like me and you like better stronger crypto, you’d be using ECC (Elliptic Curve Cryptography), and in this example I use ED25519. The -C parameter is a comment on the key, in the event you mabage multiple keys. It will create 2 files - id_rsa and id_rsa.pub.
$ ssh-keygen -t ED25519 -C "My-Test-Key"
For the newly created ED25519 keys, the filenames are namely id_ed25519 and ud_25519.pub . As you may have guessed, the file with the extension .pub is the public key file, which you will use. Copy the contents and paste it into the server’s /home/git/.ssh/authorized_keys. Alternatively you can automatically place the keys using ssh-copy-id command. At your client’s machine which you just generated the keypair…
$ ssh-copy-d -i /Users/yourleetuser/.ssh/id_ed25519 [email protected]
Note that the public keys needs to be part of git user in the server.
Now that the keys are in and you can verify by ssh into your server. It shouldn’t ask you for passwords and log you in. If there is trouble, and you have rsyslog enabled in your server, you can refer to /var/log/auth.log for authentication/ssh troubleshooting.
Now, on your machine, lets make some global configuration settings.
git config --global init.defaultBranch main
This will set the default initial branch to “main”. You can use anything for that, i.e. master, etc.
Now, lets get to the part where we create the repo. Our repo will be mydev.git
# su - git
$ mkdir mydev.git
$ cd mydev.git
$ git init --initial-branch main
Since we have already set the default initial branch, we can omit –initial-branch (unless if you want to specify a different branch).
Now at your development machine, you can start by initialise the directory at the local machine. The assumption is that SSH is running on the default port 22.
$ git clone ssh://[email protected]/home/git/mydev.git
$ cd mydev
At this point you can start adding/editing files inside the local mydev directory. Once done…
$ git add .
$ git commit -m "Initial commit"
$ git push origin main
Now your source code is synchronised to your git repository.