- Knowledge
- technology
- OOP
- Tips
- Programming
- Tips
- Tutorial
- SEO
- Ranking
- Knowledge
- Special Day
- Seo
- Bug
- Data science
- Seo
- artificial intelligence
- Machine Learning
- Robotics
- happyNewYear2021
- newYearEve
- 2021
- Automation
- Smart Home
- Career
- Best Practices
- Git
- Logging
- Web Fundamentals
- DNS
- HTTPS
- Performance
- AI Tools
- ChatGPT
- Claude
- Gemini
- Laravel
- Eloquent
- MySQL
- HTTPS
- TLS
- Web Security
- Certificates
- Developer Life
- Debugging
- Docker
- DevOps
- Transactions
- Queues
- LLMs
- AI
- AI Coding
- Developer Tools
- React Native
- Expo
- Kate PMS
- Mobile Apps
- Laravel
- Authentication
- Sanctum
- Cookies
- API Design
- Payments
- Idempotency
- DeepSeek
- Open Source AI
- LLMs
- AI News
- Git
- Version Control
- AI Coding
- Prompting
- PHP
- Checklist
- MCP
- AI Agents
- OpenAI
- Architecture
- Microservices
- Modular Monolith
- Estimation
- Developer Life
- Project Planning
- Humour
- OAuth
- OpenID Connect
- Authentication
- Embeddings
- Vector Search
- RAG
- pgvector
- OpenAI
- GPT-4.1
- Codex CLI
- Events
- Testing
- Clean Code
- Maintainability
- Code Review
- Webhooks
- API
- Security
- Claude Code
- Workflow
- AI
- LLM
- Prompt Injection
- Mobile
- React
- Networking
- TCP
- UDP
- HTTP/3
- CLAUDE.md
- AWS
- Cloud Security
- Backups
- PHPUnit
- Software Engineering
- Leadership
- Communication
- RAG
- Embeddings
- AI Engineering
- IT Infrastructure
- Networking
- Access Control
- CI/CD
- GitHub Actions
- Gemini CLI
- Claude Code
- JavaScript
- Async/Await
- Node.js
- Promises
- Security
- Cryptography
- Passwords
- MySQL
- Database
- Vibe Coding
- Software Quality
- DNS
- Code Reading
- Onboarding
- Productivity
- Background Jobs
- Developer Humour
- Estimates
- Dev Life
- JWT
- o3-mini
- DeepSeek R1
- Rate Limiting
- Kate PMS
- E-Signing
- Audit Trail
- REST
- GraphQL
- API Design
- Laravel 12
- Upgrade Guide
- Open Source
- Self-Hosting
- Task Scheduling
- Cron
- Secrets
- CORS
- PHP
- PHP-FPM
- OPcache
- GitHub Copilot
- Software Architecture
- Engineering
- TypeScript
- JavaScript
- Type Safety
- AI Security
- React Native
- Product Design
- AI Agents
- Kiro
- Queues
- Redis
- RabbitMQ
- AWS SQS
- Nginx
- Apache
- GPT-5
- gpt-oss
- Clean Code
- Architecture
- Naming
- Documentation
- Career
- ADR
- Teamwork
- Supply Chain
- Kate HRM
- HR Software
- Permissions
- System Design
- Pagination
- SSH
- Linux
- Big O
- Databases
- Laravel Boost
- MCP
- Developer Skills
- Validation
- Databases
- Indexes
- Code Quality
- Deployment
- Developer Humour
- Feature Flags
- Code Review
- Pull Requests
- Docker
- Cursor
- Authorization
- RBAC
- Gemini
- Long Context
- PHP 8.4
- Caching
- Dependency Injection
- Web Performance
- Browser
- CSS
- Database
- Migrations
- ChatGPT
- AI for Developers
- Monitoring
- On-Call
- REST
- Backend
- SQL
- NoSQL
- Database Design
- Coding Agents
- Claude 4
- API Resources
- REST API
- Load Balancing
- Scaling
- AWS
- AI Tools
- Claude
- Sora 2
- CTE
- 2FA
- TOTP
- Programming Languages
- Prompts
- Developer Workflow
- API Gateway
- APIs
- Passport
- API Auth
- Learning
- Burnout
- Developer Growth
- Web Development
- SEO
- Kate Mall
- ChatGPT Atlas
- Agent Skills
- Middleware
- Laravel 12
- Collections
- Context Window
- Monitoring
- Commit Messages
- Self Review
- Growth
- Regex
- Programming Basics
- Text Processing
- Database Design
- Normalization
- Linux
- Server Security
- Linux Foundation
- Open Standards
- Legacy Code
- Documentation
- AI Workflow
- File Uploads
- Test Data
- Hashing
- Performance
- Caching
- Enums
- Scope Creep
- Estimation
- Codex
- Gemini CLI
- Timezones
- Carbon
- Bugs
- PHP 8.5
- Gemini 3
- GPT-5.1
- Data Integrity
- Event Loop
- Async
- Opus 4.5
- AI Models
- React
- Forms
- Frontend
- Backups
- AI Images
- DALL-E
- Midjourney
- Race Conditions
- Concurrency
- Legacy Code
- Refactoring
- Senior Engineer
- Scope
- LLM
- CDN
- Web
- Sub-Agents
- Soft Deletes
- Audit Log
- Concurrency
- AI Learning
- NestJS
- AI Evals
- Policies
- SPF DKIM DMARC
- Unicode
- UTF-8
- Knowledge Graph
- Value Objects
- Technical Debt
- Feature Flags
- Laravel Pennant
- Deployment
- Copilot
- Composer
- Dependencies
- Artisan
- Automation
- AWS S3
- Object Storage
- Cloud
- Small Language Models
- Ollama
- Production
- Sessions
- HTTP
- Mentoring
- SQL
- Virtual Machines
- Web Development
- HTTP/2
- QUIC
- Web Performance
- AI Integration
- LLM API
- SOLID
- OOP
- Hosting
- Serverless
- Merge Conflicts
- Temperature
- AI Development
- Reverse Proxy
- Nginx
- Infrastructure
- Verification
- Passkeys
- WebAuthn
- Teams
- Communication
- Stakeholders
- Monorepo
- CI/CD
- Versioning
- JSON Schema
- Livewire
- Inertia
- Meetings
- Distributed Systems
- Privacy
- Full-Stack
- T-Shaped Skills
- Money
- Notifications
- Web Security
- HTTP Headers
- CSP
- Function Calling
- Load Testing
- k6
- Data Extraction
- Debugging
- WebSockets
- SSE
- Real-Time
- Laravel Reverb
- Infrastructure as Code
- Terraform
- Side Projects
- Laravel Pint
- OpenAPI
- Swagger
- UX
- Multimodal
- Jest
- Pair Programming
- APIs
- Rate Limiting
- Resilience
- Dev Humour
- Design Tokens
- JWT
- API Keys
- Sessions
- PHPStan
- Rector
- Incidents
- Reporting
- Dashboards
- Zero Trust
- IAM
- Search
- Laravel Scout
- Junior Developers
- Mentoring
- Images
- WebP
- AVIF
- Bug Reports
- Let's Encrypt
- Design Docs
- Software Design
- Observers
- Replication
- Accountability
- Data Structures
- Reliability
- LLM Memory
- Error Handling
- Payments
- Payment Gateway
- Webhooks
- PCI DSS
- Observability
- OpenTelemetry
- Personal Brand
- Writing
- Conventions
- Dates
- Scheduling
- Disaster Recovery
- Compression
- Brotli
- Deadlines
- Developer Habits
- State Machines
- Tech Roles
- UUID
- ULID
- Horizon
- Planning
- Engineering Culture
- Ownership
- Soft Skills
- Socialite
- Cost Control
- Collations
- Unicode
- Octane
- PostgreSQL
Git Mistakes and How to Undo Them (Without Making Things Worse)
About Post
There's a specific kind of silence that follows a Git command you regret. You pressed Enter, the terminal printed something short and final, and your stomach dropped before your brain caught up.
Here's the good news: Git is very hard to truly break. Almost everything you've committed can be recovered, and most mistakes have a clean, one- or two-command fix. The danger isn't the mistake. It's the panicked second command typed to fix the first one.
So here's a calm rescue guide. Find your situation, read the warning, then type.
First question: has it been pushed?
Almost every fix depends on this. The rule is simple:
- Not pushed yet: it's your private history. Rewrite it freely with
amend,resetandrebase. - Pushed to a shared branch: other people may have built on it. Don't rewrite it; add a new commit that undoes it with
revert.
Keep that in mind for everything below.
"I forgot a file" or "the commit message has a typo"
Not pushed? Amend the last commit:
git add app/Models/Lease.php
git commit --amend --no-edit # add the file, keep the message
git commit --amend -m "Fix lease renewal date calculation" # or fix the message
--amend replaces the last commit with a new one. That's fine locally, but if you already pushed, amending means you'll need a force push, which brings its own rules (see below).
"I committed to main instead of my feature branch"
Classic. Not pushed yet? Create the branch where you are, then move main back:
git branch feature/lease-renewals # new branch keeps your commit
git reset --hard HEAD~1 # move main back one commit
git switch feature/lease-renewals # carry on working here
If you made three commits by mistake, use HEAD~3. And if the feature branch already exists, switch to it and git cherry-pick <sha> the commit across before resetting main.
Warning: --hard also throws away any uncommitted changes. Run git status first, and git stash if anything is there.
"I want to undo my last commit": reset, three ways
git reset moves your branch back. The flag decides what happens to the changes from the commits you're undoing:
| Command | The commit | Its changes | Use when |
|---|---|---|---|
git reset --soft HEAD~1 | Undone | Kept, still staged | You want to recommit differently |
git reset HEAD~1 (mixed) | Undone | Kept, unstaged | You want to re-pick which files go in |
git reset --hard HEAD~1 | Undone | Thrown away | You really want it gone |
When in doubt, use --soft. You can always throw changes away later; you can't easily get uncommitted work back.
"I pushed a bad commit to a shared branch"
Don't reset. Revert. git revert creates a new commit that does the exact opposite of the bad one, so history only moves forward and nobody else's clone breaks:
git revert 4f9c2ab
git push
Reverting a merge commit needs one extra flag to say which parent is the "main line", usually the first: git revert -m 1 <merge-sha>. One gotcha worth knowing: if you later merge that same branch again, the reverted changes won't come back automatically, because Git thinks they're already merged. You'd need to revert the revert.
"I lost commits": reflog to the rescue
You did a reset --hard to the wrong place. A rebase went sideways. You deleted a branch that wasn't merged. It feels like the work is gone. It almost certainly isn't.
Git keeps a local diary of everywhere HEAD has pointed, called the reflog:
git reflog
# a1b2c3d HEAD@{0}: reset: moving to HEAD~3
# 9e8f7a6 HEAD@{1}: commit: Add renewal reminders
# ...
git branch rescue 9e8f7a6 # put a branch on the lost commit
Creating a branch is the safest move: it makes the lost commit reachable again without changing anything else. Then inspect it and merge or reset as you like.
Two more lifelines. When you delete a branch, Git prints its last commit (Deleted branch x (was 1a2b3c4)), so git branch x 1a2b3c4 brings it back. And right after a bad rebase, reset or merge, ORIG_HEAD points to where you were before: git reset --hard ORIG_HEAD.
The reflog is local to your machine and old entries do expire eventually (by default after months, not minutes), so you have plenty of time, but not forever.
The rule that makes all of this work: committed work is almost never lost; uncommitted work easily is. reset --hard, git restore and git clean on uncommitted changes have no undo. Commit early, even as "wip", and tidy up before you push.
"I need to force push": do it safely
After amending or rebasing a branch you'd already pushed, a normal push is rejected. A plain git push --force overwrites the remote branch with yours, including any commits a colleague pushed in the meantime, which are then gone from the branch.
Use the safer version:
git push --force-with-lease
It refuses to push if the remote branch has changed since you last fetched it. One subtlety: if your editor fetches in the background, your "last fetch" may include commits you've never actually looked at. Adding --force-if-includes (Git 2.30 and later) guards against that too.
And the simplest rule: only force push to your own branches, never to main or a shared branch. Most teams protect main on GitHub or GitLab so it can't happen by accident.
"I committed a secret"
The one where Git isn't the main fix. If an API key or password was pushed, assume it's been seen: rotate it first, immediately. Rewriting history afterwards (with a tool like git filter-repo) is cleanup, not a fix, because the old commit can already live in other clones, forks and CI logs.
Not pushed yet? Amend or reset it out, add the file to .gitignore, and you got lucky.
Smaller oops, quick fixes
- Staged the wrong file:
git restore --staged path/to/file. - Merge full of conflicts you're not ready for:
git merge --abort. - Rebase in progress and it's a mess:
git rebase --abort. - Not sure what you're about to lose:
git statusandgit log --oneline -5before any reset.
The rescue checklist
- Stop. Don't type the second command in a panic.
- Ask: pushed or not? Private history can be rewritten; shared history gets reverted.
- Lost something?
git reflog, then put a branch on it. - Force push only with
--force-with-lease, only on your own branch. - Commit early. Committed work is safe work.
What's the Git command that has given you that stomach-drop moment? And which one rescued you?

Be first to comment it...