Syncing GitHub to Ubuntu Server and Vice Versa

Why should you do this?

I have been working on setting up an Ansible control server to start up some automation like updating machines to the latest software. As I’m lazy i want to just login 1 place instead of ~10 places and doing the same commands.

Required Knowledge
Command Line Interface (CLI ) Basic Understanding
Knowledge about GitHub

Required Software
GitHub.com – It’s free

How to guide

Go to Github.com

Create an account
Make sure to setup 2FA, its just good practice :)
Create your first Repo by clicking on the "New"
Fill in the information and click "Create repository"

Linux Machine Setup

Login to your Linux Server
Run command in a terminal window
sudo apt install -y git

This installs git(hub) on to your machine.

Adding the SSH security key

The reason for this is because GitHub disabled password sync for private accounts.

Run command
ssh-keygen -t ed25519 -C "the email you used for github"
Let it save to its default location. "~/.ssh/"
Once done, use Cat to see the information string needed.
Run command
cat ~/.ssh/id_ed25519.pub
copy this information

This creates a SSH keypair that is needed for encrypted communication with GitHub

Back to GitHub

Top right click your profile icon
Go to Settings
In the left panel, click on "SSH and GPG Keys"
Click on "new SSH Key"
Give this a name example: "Linux server"
Paste the information in and press "Add SSH Key"

Adding your key to GitHub

Setting up the final touches

Run commands
git config --global user.name "your account name"
git config --global user.email "the email you used for GitHub"
Note:
Since I used ssh-key I won't have to add any more credentials

We need to add information to our Git otherwise it won’t know where to send files and we would get and error.
skip the “global” part if you only want to use it for this 1 folder.

In your home folder
Run Command
git clone ssh://[email protected]:443/<your profile name>/<your repo name.git>

What this does is it downloads your repository into your home folder
Now it might be empty, but don’t worry about this part. You will fill it up soon enough. 🙂

Testing it works

cd into the folder
Run command
sudo nano test.txt - Input some text
Press CTRL + S to save
Press CTRL + X to exit

Here we create a “dummy” file, just to see if we can upload files to our repository.

To upload / sync files

Inside the folder (from earlier)
Run command
git add .
git commit -m "what you did / changed"
git push
And it should saying along the lines of "

The git add . referees to files in the folder (everything)
The git commit -m “msg” is to keep track of changes with the message part.
git push > it uploads the files to the repository

Now press F5 in your browser and you should be able to see a “test.txt” file in your repository.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *