- Posted on
- • Getting Started
Mastering Git in Linux Bash: From Basics to System Configuration Tracking
- Author
-
-
- User
- Linux Bash
- Posts by this author
- Posts by this author
-
Why Revision Control Systems Matter
Imagine losing weeks of work because of a bad configuration change. Or collaborating on code where three people overwrite each other’s fixes. Revision control systems (RCS) like Git solve these nightmares by:
1. Tracking Changes: Every modification is recorded, with who made it and why.
2. Disaster Recovery: Revert to a working version instantly.
3. Collaboration: Merge contributions cleanly without chaos.
4. Accountability: Audit trails for compliance and debugging.
Without an RCS, you’re flying blind. Git—created by Linus Torvalds for Linux kernel development—is the gold standard. Let’s dive in!
Core Git Commands: Your Toolkit
All commands below run in Linux Bash. First, install Git:
sudo apt install git # Debian/Ubuntu
sudo dnf install git # Fedora
1. git clone
Download a repository (remote → local):
git clone https://github.com/user/project.git
2. git add
Stage changes for commit (track new/modified files):
git add file.txt # Stage one file
git add . # Stage all changes
3. git commit
Save staged changes with a message:
git commit -m "Fixed network config"
4. git tag
Bookmark important commits (e.g., releases):
git tag v1.0 # Lightweight tag
git tag -a v2.0 -m "Stable release" # Annotated tag
5. git checkout
Switch branches or restore files:
git checkout main # Switch to branch 'main'
git checkout v1.0 # View code at tag v1.0
git checkout -- file.txt # Discard unstaged changes
On-System Help: git help
Forget web searches! Get instant documentation:
git help add # Opens manual for `git add`
git commit --help # Browser-based help
Example: Track System Configs with Git
Use Git to safeguard /etc/
files (e.g., network/interfaces
, ssh/sshd_config
).
Step 1: Initialize a Repository
sudo mkdir /etc/git-backup && cd /etc/git-backup
sudo git init # Initialize repo in /etc/git-backup
Step 2: Stage and Commit Configs
# Copy configs to the repo (e.g., SSH config)
sudo cp /etc/ssh/sshd_config .
# Stage and commit
sudo git add sshd_config
sudo git commit -m "Initial SSH config backup"
Step 3: Track Changes Over Time
# After editing /etc/ssh/sshd_config:
sudo cp /etc/ssh/sshd_config /etc/git-backup/
sudo git diff # See changes
sudo git commit -am "PermitRootLogin no"
Recover a Broken Config:
sudo git log # Find working commit ID
sudo git checkout abcd123 -- sshd_config # Restore
sudo cp sshd_config /etc/ssh/ # Apply fixed version
Pulling Upstream Software: Tags & Branches
When cloning projects (e.g., from GitHub), use tags for releases and branches for features.
1. Fetch Tags
git fetch --tags # Get all release tags
2. Switch to a Stable Release
git tag -l # List tags (v1.0, v2.1.3, ...)
git checkout tags/v2.0.0 # Checkout the v2.0.0 release
3. Track a Development Branch
git checkout -b new-feature origin/new-feature # Local branch tracking upstream
git pull # Update from remote
4. Merge Upstream Changes
git checkout main
git pull origin main # Pull latest upstream code
Key Takeaways
- Git = Time Machine: Roll back configs, recover deleted files, audit changes.
- Tags Are Releases: Use them for stable software versions.
- Branches Are Experiments: Isolate work, then merge.
Start small—back up your dotfiles or /etc/
today. With Git in Bash, you’re not just running commands; you’re building a safety net.
Pro Tip: For sensitive data, use
git secret
(encrypt files in repo) or store configs outside/etc/
with symlinks.
Next Up: Git branching strategies for DevOps teams. Stay tuned!
# Your first Git repo in 10 seconds:
mkdir my-project && cd my-project
git init
echo "Hello, Git!" > README.md
git add README.md
git commit -m "Start my journey"