Keeping configuration in Git
This lesson is open to everyone. The other lessons open with a package: sign in or sign up, then choose a package.
Key points
- Git stores every change as a commit: who changed what, when and why.
- The main cycle:
git status→git add file→git commit -m "what and why"→git log. git diffshows changes not yet committed,git log -p filea file's history;git restore filediscards a change.- Remote repo:
git clone,git pull,git push; a branch lets you try a change without touching the main version. - Never commit secrets (passwords, keys); a
.gitignorefile keeps them out.
Why Git for an admin
| Without Git | With Git |
|---|---|
httpd.conf.bak, httpd.conf.bak2, httpd.conf.kohne | One file, all versions in the history |
| "Who changed it?" — stays a question | git log — author, date, reason |
| Reverting a bad setting — relying on memory | git revert or git restore |
| Moving settings to another server — by hand | git clone |
This approach is called Infrastructure as Code: the state of servers is described in files, and those files are kept in Git. Ansible in the next two lessons builds on this idea.
Getting started
[ali@srv-web01 ~]$ sudo dnf install -y git [ali@srv-web01 ~]$ git config --global user.name "Ali" [ali@srv-web01 ~]$ git config --global user.email "[email protected]" [ali@srv-web01 ~]$ mkdir -p ~/infra && cd ~/infra [ali@srv-web01 infra]$ git init Initialized empty Git repository in /home/ali/infra/.git/
user.name and user.email are written into every commit — they show who made the change.
The main cycle
[ali@srv-web01 infra]$ mkdir -p httpd && cp /etc/httpd/conf.d/shop.conf httpd/ [ali@srv-web01 infra]$ cp ~/bin/*.sh . [ali@srv-web01 infra]$ git status On branch main No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) disk-xeberdar.sh httpd/ port-yoxla.sh [ali@srv-web01 infra]$ git add . [ali@srv-web01 infra]$ git commit -m "İlk versiya: shop vhost və yoxlama skriptləri" [main (root-commit) 3e1a9c2] İlk versiya: shop vhost və yoxlama skriptləri 3 files changed, 41 insertions(+)
| State | Meaning | Command |
|---|---|---|
| Untracked | Git does not track the file | git add |
| Modified | Changed but not staged | git add |
| Staged | Ready for the next commit | git commit |
| Committed | In the history | — |
Following a change
[ali@srv-web01 infra]$ sed -i 's/ErrorLog .*/ErrorLog \/var\/log\/httpd\/shop-err.log/' httpd/shop.conf [ali@srv-web01 infra]$ git diff diff --git a/httpd/shop.conf b/httpd/shop.conf --- a/httpd/shop.conf +++ b/httpd/shop.conf @@ -5,6 +5,6 @@ <Directory /srv/shop> Require all granted </Directory> - ErrorLog /var/log/httpd/shop-error.log + ErrorLog /var/log/httpd/shop-err.log CustomLog /var/log/httpd/shop-access.log combined [ali@srv-web01 infra]$ git commit -am "shop: error log adı qısaldıldı" [ali@srv-web01 infra]$ git log --oneline 7b2d4f1 (HEAD -> main) shop: error log adı qısaldıldı 3e1a9c2 İlk versiya: shop vhost və yoxlama skriptləri
- is the old line, + the new line. git commit -am commits already tracked files without add.
A good commit message says what changed and why: "nginx: worker count raised to 4 — the CPU has 4 cores". Messages like "fix" or "test" say nothing a month later.
| Command | What it does |
|---|---|
git log --oneline | A short history |
git log -p file | All changes to a file |
git show 7b2d4f1 | The contents of one commit |
git blame file | Who wrote each line, in which commit |
git restore file | Discard an uncommitted change |
git revert 7b2d4f1 | A new commit that undoes a commit (the history stays) |
Keeping secrets out
[ali@srv-web01 infra]$ cat > .gitignore <<'EOF' *.key *.pem .smbcred secrets/ EOF [ali@srv-web01 infra]$ git add .gitignore && git commit -m ".gitignore: açarlar və sirlər"
Once a password, private key or token is committed, deleting the file is not enough — it stays in the history. In that case change the secret immediately. Ansible has ansible-vault for secrets (next lesson).
Remote repos and branches
For team work, a central repo (GitLab, Gitea, GitHub, or just a server reachable over SSH):
[ali@srv-web01 infra]$ git remote add origin ali@srv-db01:/srv/git/infra.git [ali@srv-web01 infra]$ git push -u origin main [ali@srv-web01 infra]$ git switch -c test-yeni-port [ali@srv-web01 infra]$ git commit -am "shop: 8090 portunda sınaq" [ali@srv-web01 infra]$ git switch main [ali@srv-web01 infra]$ git merge test-yeni-port
| Command | What it does |
|---|---|
git clone URL | Take a copy of a repo |
git pull | Bring in remote changes |
git push | Send your commits |
git switch -c name | Create a new branch and switch to it |
git merge name | Merge the branch into the current one |
A branch is for trying a change without touching the main version. In a team, a change is usually prepared on a branch and accepted into the main branch after review through a pull request (merge request).
For /etc: etckeeper
To keep all of /etc in Git automatically, there is the etckeeper package from EPEL: it commits after every dnf transaction and daily. This fully answers "who changed what in /etc".
Lab: an infra repo
On your own VM (or with configuration files in the simulator):
- Install
git, setuser.nameanduser.email. - Create the
~/infrarepo, add one configuration file and one script, and make the first commit. - Change a line in the configuration file, look at it with
git diff, and commit with a meaningful message. - Look at the history with
git log --onelineandgit log -p. - Create
.gitignoreand check that atest.keyfile does not appear ingit status.
[ali@srv-web01 infra]$ touch test.key [ali@srv-web01 infra]$ git status --short [ali@srv-web01 infra]$ git log --oneline | head -n 3
Check: there are at least three commits; test.key does not appear in git status.
Check yourself
5 questions · answers are shown instantly · 3 correct answers complete the lesson
1Which command stages a change for the next commit?
2Which command shows changes not yet committed?
3How do you undo a bad commit without erasing the history?
4A password was committed by accident. What is the most important step?
5Which command shows who wrote each line of a file?
Comments
Sign in to comment.