# Git Setup Guide for WSL2 Homelab ## The Problem We Solved When working with git in WSL2 (Windows Subsystem for Linux), you encountered a common issue: **git cannot properly manage repositories on Windows filesystems** (like `/mnt/c/Users/...`). ### Why This Happens Git requires specific POSIX file permissions and attributes that Windows' NTFS filesystem doesn't support. When git tries to create lock files (like `.git/config.lock`), it fails because: 1. Windows filesystems don't support the same permission model as Linux 2. WSL2 translates permissions, but this breaks git's lock file mechanism 3. Git needs to atomically create/rename files with specific permissions The error you saw was: ``` error: chmod on .git/config.lock failed: Operation not permitted ``` This is git saying "I can't set the proper permissions on my lock file." ## The Solution We Implemented We moved your repository to the **Linux filesystem** where git works properly, while keeping it accessible from Windows. ### What We Did 1. **Created a new directory in Linux filesystem**: `/home/jramos/homelab` - This is a "real" Linux filesystem with proper permission support - Git works perfectly here 2. **Copied all your files** from the Windows location to the Linux location 3. **Initialized git properly** in the Linux filesystem 4. **Made it accessible from Windows** via the special `\\wsl$\` network path ## Your Repository Locations ### Linux Path (Use this in WSL2 terminal) ```bash cd /home/jramos/homelab ``` ### Windows Path (Use this in File Explorer) ``` \\wsl$\Ubuntu\home\jramos\homelab ``` To access from Windows: 1. Open File Explorer 2. In the address bar, type: `\\wsl$\Ubuntu\home\jramos\homelab` 3. Press Enter 4. You can bookmark this location for easy access **Pro Tip**: You can map this as a network drive: - Right-click on "This PC" in File Explorer - Choose "Map network drive" - Use the path: `\\wsl$\Ubuntu\home\jramos\homelab` ## What About Your Old Windows Directory? You have two options: ### Option 1: Keep Windows Directory as Working Copy (Recommended) Keep using `/mnt/c/Users/fam1n/Documents/homelab` for day-to-day work, and manually sync changes to the git repository when needed: ```bash # After making changes in Windows location, sync to git repo: rsync -av --exclude='.git' /mnt/c/Users/fam1n/Documents/homelab/ /home/jramos/homelab/ # Then commit in the git repo: cd /home/jramos/homelab git add . git commit -m "Updated configurations" ``` ### Option 2: Fully Switch to Linux Location Start working exclusively in `/home/jramos/homelab`. You can delete or archive the Windows directory. ## Your First Git Commit ### Quick Method Run the helper script: ```bash cd /home/jramos/homelab ./git-first-commit.sh ``` This interactive script will: 1. Set up your git username and email (if not already configured) 2. Show you what will be committed 3. Create your initial commit 4. Explain what to do next ### Manual Method If you prefer to do it manually: ```bash cd /home/jramos/homelab # Configure your identity (one-time setup) git config --global user.name "Your Name" git config --global user.email "your.email@example.com" # Check what files will be committed git status # Add all files to staging area git add . # Create your first commit git commit -m "Initial commit: Homelab infrastructure repository" # View your commit git log ``` ## Files That Are Ignored by Git The `.gitignore` file tells git to **not** track certain files. Here's what's ignored: ### Sensitive Information - `.env` - Your actual environment configuration (contains passwords, IPs, etc.) - `*.key`, `*.pem` - Private keys and certificates - `secrets/` - Any directory for secrets - `credentials.json` - Credential files ### Large Files - `*.iso` - ISO images (too large for git) - `*.qcow2`, `*.vmdk` - Virtual machine disk images - `*.tar.gz`, `*.zip` - Compressed archives ### Temporary Files - `*.log` - Log files - `*.tmp` - Temporary files - `backups/` - Backup directories - `homelab-export-*/` - Your export directories (unless you want them tracked) ### What IS Tracked - All your scripts (`.sh` files) - Documentation (`.md` files) - `.env.example` - Template for environment configuration - Configuration files ## Common Git Commands for Beginners ### Checking Status ```bash cd /home/jramos/homelab git status # See what's changed ``` ### Making Changes and Committing ```bash # Edit your files, then... git status # See what you changed git diff # See exactly what changed (detailed) git add filename.sh # Stage a specific file git add . # Stage all changes git commit -m "Description" # Commit with a message ``` ### Viewing History ```bash git log # See all commits (detailed) git log --oneline # See commits (compact) git log --graph --oneline # See commits with graph ``` ### Undoing Changes ```bash # Undo changes in a file (before staging) git checkout -- filename.sh # Unstage a file (undo git add) git reset filename.sh # See changes in a specific commit git show # Revert to a previous version git checkout -- filename.sh ``` ### Comparing Versions ```bash # See what changed since last commit git diff # See what's staged for commit git diff --staged # Compare with a specific commit git diff ``` ## Best Practices for Your Homelab ### 1. Commit Often, Commit Early Don't wait until you have "perfect" changes. Commit each logical change: ```bash # Good commit messages: git commit -m "Add Ansible playbook for nginx configuration" git commit -m "Update Proxmox backup schedule to daily" git commit -m "Fix network interface configuration for web-server-01" ``` ### 2. Write Descriptive Commit Messages ```bash # Bad: git commit -m "updates" git commit -m "fix" # Good: git commit -m "Update GitLab VM memory from 4GB to 8GB for better performance" git commit -m "Fix nginx reverse proxy configuration for NetBox" ``` ### 3. Before Making Major Changes Create a commit first, so you can easily undo: ```bash git add . git commit -m "Checkpoint before restructuring network configuration" # Now make your risky changes # If things break: git checkout -- filename ``` ### 4. Review Before Committing ```bash git status # What files changed? git diff # What exactly changed? git add . # Stage everything git status # Double-check what's staged git commit -m "Your message" ``` ### 5. Keep .env Secure **NEVER** commit your `.env` file. It's already in `.gitignore`, but be careful: ```bash # This is safe (only adds .env.example): git add . # This would be dangerous (explicitly adds .env, bypassing .gitignore): git add -f .env # DON'T DO THIS! ``` ## Workflow Example Here's a typical workflow when you make changes: ```bash # 1. Navigate to your repository cd /home/jramos/homelab # 2. Make changes to your files nano collect-homelab-config.sh # Edit your script # 3. Test your changes ./collect-homelab-config.sh # 4. Review what changed git status git diff collect-homelab-config.sh # 5. Stage the changes git add collect-homelab-config.sh # 6. Commit with a descriptive message git commit -m "Add paranoid collection level to homelab collection script - Added new PARANOID level with complete system snapshot - Includes kernel modules, network statistics, and process list - Updated documentation in COLLECTION-GUIDE.md" # 7. View your history git log --oneline -5 # Show last 5 commits ``` ## Troubleshooting ### "I'm in the wrong directory" ```bash pwd # Where am I? cd /home/jramos/homelab # Go to the right place ``` ### "Git says 'not a git repository'" ```bash # Check if .git directory exists ls -la /home/jramos/homelab/.git # If it doesn't exist, you're in the wrong place cd /home/jramos/homelab ``` ### "I committed something I shouldn't have" ```bash # Undo the last commit but keep the changes git reset --soft HEAD~1 # Undo the last commit AND discard the changes git reset --hard HEAD~1 # CAREFUL: This deletes your work! ``` ### "I want to see what changed in a commit" ```bash git log --oneline # Find the commit hash git show # See the changes ``` ### "I accidentally changed a file" ```bash # Undo changes (before git add) git checkout -- filename.sh # Undo staged changes (after git add) git reset filename.sh git checkout -- filename.sh ``` ## Next Steps 1. **Make your first commit**: ```bash cd /home/jramos/homelab ./git-first-commit.sh ``` 2. **Start tracking changes**: Every time you modify scripts or configurations, commit them 3. **Learn as you go**: Git has many features, but you only need the basics to start 4. **Consider remote backup**: Later, you might want to push your repository to GitHub/GitLab for backup ## Why Version Control Matters for Homelabs Version control gives you: 1. **History**: See what changed and when 2. **Undo**: Revert mistakes easily 3. **Documentation**: Commit messages explain why changes were made 4. **Backup**: Your configuration history is preserved 5. **Experimentation**: Try things without fear—you can always go back Think of it as "time travel for your infrastructure configurations." ## Additional Resources ### Learning Git - **Git Documentation**: https://git-scm.com/doc - **GitHub Git Handbook**: https://guides.github.com/introduction/git-handbook/ - **Interactive Git Tutorial**: https://learngitbranching.js.org/ ### Git Cheat Sheets - https://training.github.com/downloads/github-git-cheat-sheet/ ### Advanced Topics (For Later) - Branching and merging - Remote repositories (GitHub, GitLab) - Git hooks for automation - Using git with Infrastructure as Code --- **Remember**: Git is a tool to make your life easier. Start simple, commit often, and learn as you go. You don't need to master everything at once. If you run into issues, the git community is huge and helpful. Most problems you encounter have been solved before—just search for the error message. Happy version controlling!