Fix Git Errors

Step-by-step solutions for the most common Git problems — with copy-paste commands

Panic Situations
Panic You are in 'detached HEAD' state
HEAD detached at a3f2c1b You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by switching back to a branch.

This happens when you checkout a commit hash, tag, or remote branch directly instead of a local branch. Any commits you make here are "floating" and can be lost.

  1. 1Check where HEAD is pointing:
git log --oneline -5
  1. 2If you have no new commits to save, just switch back to your branch:
git checkout main
  1. 3If you made commits you want to keep, create a new branch from here first:
git checkout -b my-recovery-branch
  1. 4Then merge or rebase that branch into main as needed.
💡 Tip: Always checkout branch names (e.g. git checkout main) rather than commit hashes to avoid detached HEAD.
Panic I need to undo my last commit
Committed too early, wrong branch, or included the wrong files?

There are two safe ways to undo a commit depending on whether you want to keep your changes.

  1. 1Soft reset — undo the commit but keep changes staged (safest):
git reset --soft HEAD~1
  1. 2Mixed reset — undo the commit and unstage changes (files still modified):
git reset HEAD~1
  1. 3Hard reset — undo the commit AND discard all changes permanently:
git reset --hard HEAD~1
⚠️ Danger: --hard permanently deletes your uncommitted changes. There is no undo. Use --soft unless you are certain.
💡 Already pushed? Use git revert HEAD instead — it creates a new commit that undoes the last one without rewriting history.
Merge & Rebase
Merge CONFLICT (content): Merge conflict in <file>
Auto-merging src/app.js CONFLICT (content): Merge conflict in src/app.js Automatic merge failed; fix conflicts and then commit the result.

Git couldn't automatically merge two branches because the same lines were changed differently in each.

  1. 1See which files have conflicts:
git status
  1. 2Open each conflicted file. Look for conflict markers and edit to keep the correct code:
<<<<<<< HEAD your changes here ======= incoming changes here >>>>>>> feature-branch
  1. 3After editing, mark each file as resolved:
git add src/app.js
  1. 4Complete the merge:
git commit
💡 Tip: Use git mergetool to open a visual diff tool, or use VS Code's built-in merge editor for a cleaner experience.
Merge Rebase went wrong — how do I abort?
error: could not apply a3f2c1b... my commit message hint: Resolve all conflicts manually, mark them as fixed with hint: "git add/rm <conflicted_files>", then run "git rebase --continue".

A rebase in progress with conflicts can be safely aborted to restore your branch to its pre-rebase state.

  1. 1Abort the rebase entirely and go back to where you started:
git rebase --abort
  1. 2Or, if you want to fix conflicts and continue:
git add .
git rebase --continue
  1. 3To skip a problematic commit entirely:
git rebase --skip
💡 Tip: git rebase --abort is always safe — it fully restores your branch to its original state before the rebase started.
Merge When to use reset vs revert?

Both undo changes, but they work very differently and are suited to different situations.

  1. 1Use git revert when the commit has already been pushed to a shared branch. It creates a new commit that undoes the changes — safe for team repos:
git revert HEAD
git revert <commit-hash>
  1. 2Use git reset only on local, unpushed commits. It rewrites history:
git reset --soft HEAD~1
⚠️ Never use git reset on commits that others have already pulled. It rewrites history and will cause conflicts for your teammates.
💡 Rule of thumb: Pushed = revert. Local only = reset.
Remote & Push
Remote error: failed to push some refs — push rejected
! [rejected] main -> main (fetch first) error: failed to push some refs to 'https://github.com/user/repo.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. Integrate the remote changes before pushing again.

Someone else pushed to the remote branch since your last pull. You need to integrate their changes first.

  1. 1Pull the latest changes and merge:
git pull origin main
  1. 2Or pull with rebase for a cleaner history:
git pull --rebase origin main
  1. 3Resolve any conflicts, then push:
git push origin main
⚠️ Do not use git push --force on shared branches. It overwrites others' work. Only use force-push on your own feature branches.
Remote SSL certificate problem: unable to get local issuer certificate
fatal: unable to access 'https://github.com/user/repo.git/': SSL certificate problem: unable to get local issuer certificate

Git can't verify the SSL certificate of the remote server. Common on corporate networks with custom CA certificates or misconfigured Git installs.

  1. 1Update your CA certificate bundle (preferred fix on Linux/Mac):
git config --global http.sslCAInfo /path/to/ca-bundle.crt
  1. 2On Windows, update Git for Windows to the latest version — it bundles an updated CA store.
  1. 3If on a corporate network, ask IT for the internal CA certificate and add it to Git's trust store.
  1. 4Temporary workaround only (not for production):
git config --global http.sslVerify false
⚠️ Disabling SSL verification is a security risk. Only use it as a temporary diagnostic step, never permanently.
Working Tree
Common warning: LF will be replaced by CRLF / CRLF line endings
warning: LF will be replaced by CRLF in file.txt. The file will have its original line endings in your working directory.

Windows uses CRLF line endings, Unix/Mac use LF. Git is warning you about automatic conversion. This can cause noisy diffs and cross-platform issues.

  1. 1On Windows — auto-convert on checkout, commit as LF (recommended for cross-platform teams):
git config --global core.autocrlf true
  1. 2On Linux/Mac — commit as-is, no conversion:
git config --global core.autocrlf input
  1. 3For team consistency, add a .gitattributes file to your repo root:
* text=auto eol=lf
💡 Tip: A .gitattributes file overrides individual developer settings and is the most reliable way to enforce consistent line endings across a team.
Common Untracked files cluttering my working tree
Untracked files: (use "git add <file>..." to include in what will be committed) node_modules/ .env dist/

Files Git doesn't know about yet. You either want to track them, ignore them, or remove them.

  1. 1Add files you want to track:
git add <filename>
  1. 2Ignore files permanently by adding them to .gitignore:
echo "node_modules/" >> .gitignore
  1. 3Preview what would be deleted before cleaning:
git clean -n
  1. 4Remove all untracked files (irreversible):
git clean -fd
⚠️ git clean -fd permanently deletes files. Always run git clean -n first to preview what will be removed.
Common Stash conflict — cannot apply stash
error: Your local changes to the following files would be overwritten by merge: src/app.js Please commit your changes or stash them before you merge. Aborting

Your stashed changes conflict with the current state of the working tree. Git can't apply the stash cleanly.

  1. 1List your stashes to find the right one:
git stash list
  1. 2Try applying with conflict markers instead of popping:
git stash apply stash@{0}
  1. 3Resolve the conflict markers in the affected files, then stage them:
git add src/app.js
  1. 4Once resolved, drop the stash entry:
git stash drop stash@{0}
💡 Tip: Use git stash apply instead of git stash pop when you're unsure — it keeps the stash entry until you manually drop it, so you can retry if something goes wrong.

About This Tool

The Fix Git Errors tool provides quick, copy-paste solutions for the most common Git problems developers encounter. Each error includes the exact error message, a clear explanation of why it happens, and step-by-step commands to fix it. Whether you're stuck in a detached HEAD, need to undo a commit, or are resolving a merge conflict, this guide has you covered.

When to Use Each Fix

🚨 Panic Situations

Detached HEAD and "undo last commit" are urgent. Stop, don't push or force anything. Use the recovery steps to safely get back to a clean state.

🔀 Merge & Rebase

Conflicts are normal. Use the step-by-step conflict resolution process. Know when to abort (git rebase --abort) vs continue (git rebase --continue).

📤 Push & Remote

Push rejected means someone else pushed first. Pull/merge before pushing again. Never --force on shared branches.

🧹 Clean Working Tree

Untracked files, stash conflicts, line ending issues — these are local cleanup tasks. Use git clean carefully (preview first).

Frequently Asked Questions

What's the difference between git reset and git revert?

git reset moves the branch pointer backward, effectively deleting commits from the branch history. It rewrites history and should only be used on commits that have not been pushed. git revert creates a new commit that undoes the changes of a previous commit — it's safe for shared branches because it doesn't rewrite history. Rule: pushed = revert. Local-only = reset.

How do I recover a lost commit after git reset --hard?

Don't panic — the commit isn't immediately deleted. Use git reflog to see the HEAD movement history. Find the commit hash before the reset, then either git reset --hard <hash> to go back or git checkout -b recovery <hash> to create a new branch at that point. git reflog entries typically expire after 90 days.

When should I use git reset --soft vs --mixed vs --hard?

--soft undoes the commit but keeps all changes staged — use when you want to recommit with a different message or additional changes. --mixed (default) undoes the commit and unstages changes but keeps the files modified — use when you want to re-stage selectively. --hard discards both commit and all uncommitted changes — use only when you're certain you want to throw away work.

Is this tool free?

Yes, completely free with no sign-up required.

Related Tools