Day 7: Troubleshooting and Expert Techniques

Troubleshooting and Expert Techniques

Git is a powerful version control system, but sometimes things go wrong — commits get lost, files get deleted, or merge conflicts spiral out of control. This guide covers emergency recovery, conflict resolution, repository maintenance, and advanced configuration so you can handle those “Oh no!” moments like a pro.


1. Emergency Git Recovery

Recovering Lost Commits with reflog

Git stores a hidden history of everything you’ve done, called the reflog. This means even commits not on any branch can often be recovered.

# View recent branch history (all changes to HEAD)
git reflog

# View with readable timestamps
git reflog --date=iso

# Restore your HEAD to a previous state
git reset --hard HEAD@{5}

# Create a branch from an old commit
git branch recovery-branch abc123d

# Find unreachable commits
git fsck --lost-found

💡 Tip: Use reflog first whenever you think you’ve “lost” commits.


Recovering Deleted Files

If a file is deleted, you can still find and restore it.

# Find commit where file was deleted
git log --oneline --follow -- deleted_file.py

# Show the last commit that had the file
git log -1 --name-status -- deleted_file.py

# Restore from specific commit
git checkout abc123d -- deleted_file.py

# Restore from before deletion
git checkout HEAD~1 -- deleted_file.py


Undoing Different Types of Changes

ScenarioCommandKeeps Changes?
Undo last commit, keep changes stagedgit reset --soft HEAD~1
Undo last commit, keep changes unstagedgit reset HEAD~1
Undo last commit and discard changesgit reset --hard HEAD~1
Undo changes to one filegit checkout HEAD -- filename.py
Safely undo a commit in shared historygit revert abc123d

2. Advanced Conflict Resolution

Using Merge Tools

Merge tools help visualize and resolve conflicts faster.

# Use Vim as merge tool
git config --global merge.tool vimdiff

# Use VS Code as merge tool
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd 'code --wait $MERGED'

# Launch merge tool during conflict
git mergetool


Understanding a Three-Way Merge

When a conflict occurs, Git keeps three versions of the file:

git show :1:filename.py  # Common ancestor
git show :2:filename.py  # Current branch (ours)
git show :3:filename.py  # Incoming branch (theirs)

Knowing this helps resolve conflicts with precision.


3. Repository Maintenance & Optimization

Cleaning Up

# Preview untracked files to delete
git clean -n

# Delete them for real
git clean -fd

# Optimize repository
git gc --aggressive

# Check repository integrity
git fsck

# Show repository size stats
git count-objects -v

# Remove stale remote branches
git remote prune origin

# Delete merged local branches (except main/master)
git branch --merged | grep -v "\*\|main\|master" | xargs -n 1 git branch -d


Archiving & Backup

# Create project archive
git archive --format=zip --output=backup-v1.0.zip HEAD

# Archive a specific tag
git archive --format=tar.gz --output=release.tar.gz v1.0.0

# Bundle entire repo for offline transfer
git bundle create repo.bundle --all


Handling Large Files with Git LFS

git lfs track "*.psd"
git lfs track "*.zip"
git add .gitattributes
git commit -m "Add LFS tracking for large files"

Git LFS keeps large files outside the main repo to avoid bloating history.


4. Performance Optimization

Shallow Clones

Clone large repositories faster by limiting history:

git clone --depth 1 https://github.com/large-project/repo.git
git fetch --unshallow  # Fetch full history later

Sparse Checkout

Download only specific folders in huge repositories:

git config core.sparseCheckout true
echo "frontend/*" > .git/info/sparse-checkout
echo "shared/*" >> .git/info/sparse-checkout
git read-tree -m -u HEAD


5. Advanced Git Configuration

Useful Aliases

git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual '!gitk'
git config --global alias.hist 'log --oneline --graph --decorate --all'

Extra Settings

git config --global diff.algorithm patience      # Better diff output
git config --global help.autocorrect 1           # Auto-correct typos
git config --global core.compression 9           # Max compression
git config --global pack.threads 0               # Use all CPU threads


Hands-On Exercise

  1. Create two branches with conflicting changes.
  2. Merge them and resolve using git mergetool.
  3. Simulate losing a commit, then recover it using reflog.
  4. Set up aliases and configs to speed up your workflow.

Takeaway:
Once you know these techniques, Git stops being scary — you’ll recover lost work, resolve conflicts, and keep your repository fast and clean like an expert.

You Might Also Like

🛠️ Recommended Tools for Developers & Tech Pros

Save time, boost productivity, and work smarter with these AI-powered tools I personally use and recommend:

1️⃣ CopyOwl.ai – Research & Write Smarter
Write fully referenced reports, essays, or blogs in one click.
✅ 97% satisfaction • ✅ 10+ hrs saved/week • ✅ Academic citations

2️⃣ LoopCV.pro – Build a Job-Winning Resume
Create beautiful, ATS-friendly resumes in seconds — perfect for tech roles.
✅ One-click templates • ✅ PDF/DOCX export • ✅ Interview-boosting design

3️⃣ Speechify – Listen to Any Text
Turn articles, docs, or PDFs into natural-sounding audio — even while coding.
✅ 1,000+ voices • ✅ Works on all platforms • ✅ Used by 50M+ people

1 thought on “Day 7: Troubleshooting and Expert Techniques

  1. This is such a valuable article! 👏 I really like how you’ve managed to explain the topic in a clear and practical way—it feels authentic and easy to relate to. Reading it gave me some new perspectives that I can actually apply. I’m especially interested in content like this because at meinestadtkleinanzeigen.de we’re running a classifieds and directory platform in Germany that connects people with services, businesses, and opportunities across many categories. Insights like yours remind me how powerful it is when knowledge and connections come together. Thanks for sharing—looking forward to more of your work! 🚀

Comments are closed.