github - How to sync your server configs with a repository

You can source control the configuration file sin your server.

Let's create a github repository :

https://github.com/****/linux-home-svr2.git

We now need to initialise our root directory, but we need to first create a .gitignore file to ignore  all the files apart from the configurations we want to sync with our repository :

tiago@home-svr2:/$ cd /
tiago@home-svr2:/$ 
tiago@home-svr2:/$ sudo apt install git
tiago@home-svr2:/$ sudo touch .gitignore
tiago@home-svr2:/$ sudo vim .gitignore 
tiago@home-svr2:/$ cat .gitignore 
# Exclude all files apart from the congifuration files to sync with the repo.
/*
!/root
/root/*
!/root/.ssh
tiago@home-svr2:/$ sudo touch /root/.ssh/config
tiago@home-svr2:/$ sudo git add .
tiago@home-svr2:/$ sudo git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   root/.ssh/config

We now need to commit our first file to test the sync :

tiago@home-svr2:/$ git remote add remote-linux-home-svr2 https://github.com/****/linux-home-svr2.git
tiago@home-svr2:/$ git remote -v
remote-linux-home-svr2  https://github.com/****/linux-home-svr2.git (fetch)
remote-linux-home-svr2  https://github.com/****/linux-home-svr2.git (push)

After committing you need to move to the main branch :

  tiago@home-svr2:/$ sudo git branch -M main

Finally we push the changes :

tiago@home-svr2:/$ sudo git push -u remote-linux-home-svr2 main
Username for 'https://github.com': ********
Password for 'https://******@github.com': 
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (5/5), 297 bytes | 297.00 KiB/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To https://github.com/****/linux-home-svr2.git
 * [new branch]      main -> main
Branch 'main' set up to track remote branch 'main' from 'remote-linux-home-svr2'.