惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
有赞技术团队
有赞技术团队
J
Java Code Geeks
H
Hackread – Cybersecurity News, Data Breaches, AI and More
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Hugging Face - Blog
Hugging Face - Blog
人人都是产品经理
人人都是产品经理
酷 壳 – CoolShell
酷 壳 – CoolShell
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
Check Point Blog
博客园 - 【当耐特】
The GitHub Blog
The GitHub Blog
Recent Announcements
Recent Announcements
The Cloudflare Blog
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
Vercel News
Vercel News
IT之家
IT之家
MyScale Blog
MyScale Blog
博客园_首页
Martin Fowler
Martin Fowler
WordPress大学
WordPress大学
罗磊的独立博客

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Emergency Guide: Repairing Git Repositories After a Power...
Ikenna Ene · 2026-06-20 · via DEV Community
Cover image for Emergency Guide: Repairing Git Repositories After a Power Outage

Ikenna Ene

Power outages, system crashes, or forced shutdowns during active Git operations (like committing, pulling, or pushing) often result in corrupted metadata files. This happens because the file-writing process is abruptly cut off mid-stream, leaving files blank, filled with null bytes, or locked.

🚨 Common Error Symptoms

  • warning: ignoring broken ref refs/heads/main
  • fatal: cannot lock ref 'HEAD': unable to resolve reference...
  • error: bad signature 0x00000000 / fatal: index file corrupt

🛠️ Step-by-Step Recovery Procedure

Step 0: Create an Emergency Backup

Before running any recovery commands, secure your current project state.

bash

cp -r .git .git_backup_corrupted

Step 1: Fix the Corrupted Index (Staging Area)

If Git throws fatal: index file corrupt or bad signature, your temporary staging cache is broken. Delete it and force Git to rebuild it.

bash

# 1. Remove the corrupted index file
rm -f .git/index

# 2. Reset the index back to your last known local state
git reset

Step 2: Clear Leftover System Lock Files

Abrupt shutdowns leave behind lock (.lock) files that prevent Git from updating references. Clear them out manually.

bash

rm -f .git/refs/heads/main.lock
rm -f .git/HEAD.lock
rm -f .git/index.lock

Step 3: Repair the Broken Branch Reference

If Git states it cannot resolve refs/heads/main because it is broken, the branch file itself is physically corrupted.

Option A: Restore using the Remote Server (Fastest & Safest)

If your branch exists on GitHub/GitLab, delete the broken local file and recreate it using the server's history.

bash

# 1. Delete the physically corrupted branch file
rm -f .git/refs/heads/main

# 2. Recreate the file pointing to your remote tracking branch
git update-ref refs/heads/main refs/remotes/origin/main

Option B: Restore using Local Logs (If you have unpushed commits)

If you made local commits right before the crash that weren't pushed online, find the last valid commit ID from Git's transaction logs.

bash

# 1. View the last 2 transaction events
tail -n 2 .git/logs/refs/heads/main

# 2. Identify the last complete line. Copy the SECOND 40-character SHA hash on that line.
# 3. Manually overwrite the broken reference file with that hash:
echo <COPIED_40_CHARACTER_HASH> > .git/refs/heads/main

Step 4: Verify and Resync Your Code

Once the metadata is repaired, check the repository health and recover your files.

bash

# 1. Check repository health
git status

# 2. Pull down any remote changes to sync up
git pull origin main

# 3. Stage and re-commit any work that was interrupted (will show as modified/untracked)
git add .
git commit -m "Recovered work after power outage"
git push origin main

💡 Root Cause Analysis & Prevention

Why does this happen?

Git updates files like .git/index and .git/refs/heads/main by writing out a temporary file first, then swapping it with the original. If power cuts during the swap or the write, the file gets truncated to 0 bytes or filled with null characters (\0\0\0), which Git cannot parse.

Best Practices to Prevent Data Loss

  1. Use an Uninterruptible Power Supply (UPS): If you are on a desktop setup prone to power cuts, a UPS gives you 15-20 minutes to save work and shut down cleanly.

  2. Commit and Push Frequently: The more often you push to a remote server, the less local transaction history you risk losing during a crash.

  3. Avoid Hard Reboots During Terminal Tasks: Never force-close your terminal or shut down your machine while a Git operation is actively running in the background.

🏁 Conclusion

Experiencing a power outage mid-development can be alarming, but it rarely results in permanent data loss. Because Git separates your actual source code files from its internal tracking metadata, a crash almost always corrupts the tracking pointers rather than your work. By systematically clearing out corrupted files (index, locks, or broken refs) and pointing Git back to a known valid commit, you can safely restore your environment and resume work without missing a beat.