Dev Tool

Git Errors — What Went Wrong and How to Fix It

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

AllOmnitools Editorial Team
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.
Panic I did git reset --hard and lost my work
git reset --hard HEAD~3 # oops, lost commits

Don't panic! Git doesn't immediately delete lost commits. You can recover using the reflog.

  1. 1View the reflog (your HEAD history):
git reflog
  1. 2Find the commit hash before the reset (e.g., abc1234). Then recover:
git checkout -b recovery-branch abc1234
Tip: The reflog keeps a record of all HEAD movements for about 90 days. Act quickly.
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.
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.
Remote fatal: refusing to merge unrelated histories
fatal: refusing to merge unrelated histories

This happens when you try to merge two branches that have no common commit ancestry (e.g., a new repo with an existing one).

  1. 1If you are sure you want to merge (e.g., initializing a project with an existing history), use the --allow-unrelated-histories flag:
git pull origin main --allow-unrelated-histories
  1. 2Or when merging a branch:
git merge other-branch --allow-unrelated-histories
Tip: Use this only when you understand the consequences — you will merge two completely different commit histories.
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.
Common Submodule errors — not checked out or dirty
fatal: Not a git repository: ../.git/modules/path/to/submodule

Submodules often get out of sync after switching branches or pulling changes.

  1. 1Initialize and update all submodules recursively:
git submodule update --init --recursive
  1. 2To update all submodules to their latest remote commit:
git submodule update --remote --merge
  1. 3If submodule has uncommitted changes you want to discard:
git submodule foreach --recursive git reset --hard
Tip: After a git pull, always run git submodule update --init --recursive to sync submodules.

The most common Git errors and exactly how to fix them

Git errors range from mildly confusing to genuinely alarming. Most of them are recoverable — even the ones that look catastrophic. Here are the ones you'll hit most often and the commands that fix them.

Error reference

Error What it means Fix
rejected — non-fast-forwardRemote has commits your local branch doesn'tgit pull --rebase then push
CONFLICT (content): Merge conflictSame lines changed in both branchesEdit conflicted files, remove markers, then git add + git commit
HEAD detached at ...You checked out a commit, not a branchgit checkout -b new-branch-name
fatal: not a git repositoryRunning git outside a repo foldercd into your project folder first
nothing to commit, working tree cleanNo staged or unstaged changesCheck git status — you may be on the wrong branch
Permission denied (publickey)SSH key not added to GitHub/GitLabRun ssh-add ~/.ssh/id_rsa or add key to your Git host

Undoing things in Git

  • git commit --amend — fix the last commit message or add a forgotten file (only safe before pushing)
  • git reset HEAD~1 — undo last commit, keep changes staged
  • git reset --hard HEAD~1 — undo last commit, discard changes (destructive)
  • git revert HEAD — safe undo: creates a new commit that reverses the last one (safe to push)
  • git stash — temporarily shelve uncommitted changes so you can switch branches

Complete Developer Toolkit

Git errors often occur alongside other development problems. The first tool to pair with this guide is our Git command cheat sheet — once you understand what went wrong, you need the exact commands to fix it. Our diff checker is invaluable when resolving merge conflicts — paste both versions of a conflicted file to see exactly what changed and decide which lines to keep. The regex tester helps you write git log --grep patterns or .gitignore glob patterns to filter commits or exclude files.

For diagnosing authentication failures when pushing to remote repositories, our Base64 encoder decodes credentials stored in git config, and our SSL expiry checker verifies the HTTPS certificate on your git server. Our DNS lookup confirms your git host domain is resolving correctly when you get connection errors. The Linux terminal cheat sheet covers the shell commands you need when running git in scripts or CI pipelines. Our JSON formatter validates JSON config files in your repo like package.json or CI pipeline configs. For cron-scheduled git jobs, our cron generator builds the schedule expressions correctly.

FAQ

Create a new branch pointing to your current commit: git checkout -b my-feature. Then reset main back: git checkout main && git reset --hard origin/main. Your commits are now only on the new branch.