Part 4 · Operations and troubleshooting · Lesson 04 · 55 minutes
Track changes with Git
- Record, compare and undo changes in a Git repository, and work on a branch
- Merge two branches, resolve a conflict, tag a release and share the work through a bare repository
- Remove a committed secret from the whole history and say why you must also rotate it
The lab folder as it stands after this lesson. Unzip it in ~/krlab.
More
The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.
Scenario
Your team keeps its runbooks and server settings in plain files, and two people edited the same file last week. One change was lost, and nobody can say who made the other. Your lead wants the files in Git, the tool that records every change, who made it and why. You learn the daily commands in a practice repository, you make the two people's changes meet, and you handle the worst mistake: a password that was committed.
Recall
In lesson 3, why is a YAML file kept in Git better than a list of commands?
The file is one exact description of the service, and Git records each change to it. A colleague starts the same service with one command, and you can compare versions and go back.
Lesson 10 of Part 1 told you to give a secret file mode 600. Why is the mode not enough once the file is in Git?
Git stores the content, and every copy of the repository holds it. The mode protects one file on one disk. Anyone who can read the repository can read the secret.
The idea
A repository is a folder with a hidden folder .git that holds the history. A commit is a saved state of all the tracked files, with a message, an author and a time. A change moves through three places: the working tree (your files), the staging area (what goes into the next commit) and the history (the commits). A branch is a movable name for a line of commits. A remote is another copy of the repository, such as a server. A bare repository has no working tree: it is only the history, and it is what a server holds.
- Working tree → Staging area: git add picks the changes
- Staging area → History: git commit saves them
- History → Remote: git push shares them
| Command | What it does |
|---|---|
git status --short | Shows changed and new files in two letters and a name |
git add FILE | Puts the change of a file into the staging area |
git commit -m "text" | Saves the staged changes as a commit |
git log --oneline | Lists commits, one line each |
git diff | Shows changes that are not staged yet (--staged: those that are) |
Worked example
The hashes (the short ids of commits) and the times differ on your machine. Install the helper of step 9 now: sudo apt install -y git-filter-repo.
1. Make a repository and say who you are
git init -b main NAME makes a repository with the first branch main. git config sets an option. Without name and address Git refuses to commit. Without --global the setting applies to this repository only.
cd ~/krlab/linux-operations
git init -b main krlab-repo
cd krlab-repo
git config user.name "You"
git config user.email "you@example.invalid"Initialized empty Git repository in /home/you/krlab/linux-operations/krlab-repo/.git/2. Make the first commit and read the log
printf 'Runbook\nstep 1: check the service\nstep 2: read the log\n' > runbook.txt
git status --short
git add runbook.txt
git commit -m "Add the runbook"
git log --oneline?? runbook.txt
[main (root-commit) dcec09c] Add the runbook
1 file changed, 3 insertions(+)
create mode 100644 runbook.txt
dcec09c Add the runbook?? means "not tracked yet". After git add, the status would show A for a new file in the staging area.
3. See a change before you commit it
sed -i 's/OLD/NEW/' FILE replaces text in a file in place (the form '1s/.*/TEXT/' replaces the whole of line 1). git diff shows the change: a line with - is removed and a line with + is added. git commit -a stages every tracked file that changed, so -am adds and commits in one step. A new file still needs git add.
sed -i 's/step 2: read the log/step 2: read the journal/' runbook.txt
git diff
git commit -am "Name the journal"@@ -1,3 +1,3 @@
Runbook
step 1: check the service
-step 2: read the log
+step 2: read the journal
[main 69fa45c] Name the journal
1 file changed, 1 insertion(+), 1 deletion(-)4. Work on a branch and merge it
git switch -c NAME makes a branch and moves to it. git switch NAME moves to an existing one. git merge NAME brings a branch into the one you are on. When main did not move meanwhile, Git only moves the name forward: a fast-forward.
git switch -c fix-title
sed -i '1s/.*/Runbook for the web service/' runbook.txt
git commit -qam "Better title"
git switch main
git merge fix-titleSwitched to a new branch 'fix-title'
Switched to branch 'main'
Updating 69fa45c..692bf0b
Fast-forward
runbook.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)5. Meet a conflict and resolve it
Two people change the same line on two branches. Git cannot choose, so it stops and marks the file. Make both changes, then merge.
git switch -c reword
sed -i 's/step 1: check the service/step 1: check that the service runs/' runbook.txt
git commit -qam "Reword step 1"
git switch main
sed -i 's/step 1: check the service/step 1: run systemctl status/' runbook.txt
git commit -qam "Use the command in step 1"
git merge reword
cat runbook.txtAuto-merging runbook.txt
CONFLICT (content): Merge conflict in runbook.txt
Automatic merge failed; fix conflicts and then commit the result.
Runbook for the web service
<<<<<<< HEAD
step 1: run systemctl status
=======
step 1: check that the service runs
>>>>>>> reword
step 2: read the journalGit marked both sides:
Edit the file so that only the final text stays, with none of the three marker lines. Then git add marks it as resolved and git commit --no-edit ends the merge with the message that Git prepared.
printf 'Runbook for the web service\nstep 1: run systemctl status and read the answer\nstep 2: read the journal\n' > runbook.txt
git add runbook.txt
git commit --no-edit
git log --oneline --graph* d3caf5c Merge branch 'reword'
|\
| * 447aafb Reword step 1
* | 0a53b34 Use the command in step 1
|/
* 692bf0b Better title
* 69fa45c Name the journal
* dcec09c Add the runbook6. Undo a change and tag a release
git restore FILE throws away your changes of a file that are not committed. git revert --no-edit COMMIT makes a new commit that undoes an older one, and keeps the history. git tag -a NAME -m "text" puts a fixed name on a commit, for a release.
echo "oops" >> runbook.txt
git restore runbook.txt
tail -n 1 runbook.txt
echo "step 3: reboot the server" >> runbook.txt
git commit -qam "Add step 3"
git revert --no-edit HEAD
git tag -a v1.0 -m "first release"
git tagstep 2: read the journal
[main be2f68a] Revert "Add step 3"
Date: Sat Nov 21 13:34:54 2026 +0000
1 file changed, 1 deletion(-)
v1.0Use revert for a commit that others may have. git reset --hard COMMIT moves the branch back and drops the later commits: use it only for work that nobody has.
7. Share through a bare repository
A bare repository is the "remote" for this lab. git remote add NAME PATH records where it is. git push -u origin main --tags sends the branch and the tags, and -u remembers the pair. git clone SOURCE FOLDER makes a full copy. Give the bare repository the branch name main, so that a clone checks out something.
cd ~/krlab/linux-operations
git init -q --bare -b main remote.git
cd krlab-repo
git remote add origin ../remote.git
git push -u origin main --tags
git clone ../remote.git ../krlab-clone
git -C ../krlab-clone log --oneline | head -n 2To ../remote.git
* [new branch] main -> main
* [new tag] v1.0 -> v1.0
branch 'main' set up to track 'origin/main'.
Cloning into '../krlab-clone'...
done.
be2f68a Revert "Add step 3"
ab48ad9 Add step 3git -C FOLDER runs a Git command as if you were in that folder.
8. Commit a secret by mistake
A colleague adds a settings file with a key and pushes it. A second person pulls it into the clone. Then you notice. git log -S TEXT finds the commits that added or removed that text.
printf 'API_KEY=kr-demo-9f2c\n' > .env
git add .env
git commit -qm "Add settings"
git push -q origin main
git -C ../krlab-clone pull -q
git log --oneline -S API_KEYf1ed4ca Add settingsA new commit that deletes the file is not enough: the key stays in the older commit, and git show prints it.
9. Remove it from the whole history
git filter-repo rewrites the history. --invert-paths --path .env keeps everything except that path, and --force allows it on a repository that is not a fresh clone. It removes the remote on purpose, so you add it again, and a push of rewritten history needs --force.
git filter-repo --invert-paths --path .env --force
git remote add origin ../remote.git
git push --force origin main
git log --oneline --all -S API_KEY | wc -l
git -C ../krlab-clone log --oneline -S API_KEYTo ../remote.git
+ f1ed4ca...be2f68a main -> main (forced update)
0
f1ed4ca Add settingsTwo more counting tools, for "Your turn": git rev-list --count main counts the commits that lead to main, and a path after --, as in git log --oneline --all -- .env, keeps only the commits that touch that path.
filter-repo prints a few progress lines before the push, and they are left out. Your repository is clean, but the clone still holds the secret, and so does every other copy and every backup. Rotate the secret: make a new key in the service that issued it, and disable the old one. Rewriting history is hygiene. Rotation is the fix.
10. Keep secrets out with .gitignore
A file .gitignore lists names that Git must not track. Write the new key in .env and ignore the file.
printf 'API_KEY=kr-demo-new\n' > .env
printf '.env\n*.log\n' > .gitignore
git status --short
git add .gitignore
git commit -qm "Ignore secrets and logs"
git check-ignore -v .env?? .gitignore
.gitignore:1:.env .env.env no longer shows as new, and git check-ignore -v names the rule that hides it.
Your turn
Work in a new repository ~/krlab/linux-operations/rota, with main as the first branch.
- Commit a file
team.txtthat holds three lines:ann,bobandcy. - On a branch
swap, change line 2 tobeaand commit. Onmain, change line 2 tobenand commit. - Merge
swapintomain, resolve the conflict so that line 2 isbea, and commit the merge. Tag that commitv1. - Then commit a file
.envthat holdsTOKEN=abc123, by mistake. Remove.envfrom the whole history. - Write
TOKEN=new456in.envagain, and ignore the file with a.gitignorethat you do not commit yet.
Check yourself
You committed a typo in the message of the last commit, and have not pushed it. What do you do?
git commit --amend -m "new text" replaces the last commit. It changes the commit id, so do it only before you push.
What is the difference between git revert and git reset --hard?
revert adds a new commit that undoes an old one, and the history stays whole, so it is safe for shared branches. reset --hard moves the branch back and discards the later commits and your uncommitted changes.
You removed a secret from history and force-pushed. Is the incident over?
No. Others cloned or pulled it, and caches and backups keep copies. Treat the secret as public: rotate it, check the logs of the service for use of the old one, and tell the owner of the service.
Why add .env to .gitignore after the secret is already committed?
It does not help for a file that Git already tracks. After the history was cleaned, the file is untracked again, and the rule stops the next accident.
Field notes
- Commit small, with a message that says why.
git logis the diary of the system, and a reader at three in the morning needs it. - Never use
git push --forceon a shared branch.--force-with-leaserefuses when someone else pushed meanwhile. - A secret scanner (a hook or a step of the pipeline of lesson 5) catches a key before it reaches the server. Scan the history of a repository that you take over.
- Sign commits and tags with GnuPG (lesson 6 of Part 3) when the source of a release matters:
git tag -s,git commit -S. - Keep the remote small: do not commit large binary files, images of disks or database dumps. Git keeps every version for ever.