Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Concepts

Common Git error messages

Decode the Git errors you meet most often: not a git repository, rejected push, divergent branches, merge conflicts, dirty tree, detached HEAD and more.

Git error messages are terse, but they are usually precise: they name the rule you tripped over and often print the command that fixes it. The trick is to read the first line (the cause) and the hint lines (the remedy) instead of the whole wall of text. Most errors fall into a few families: you are in the wrong place, Git refuses to overwrite work it cannot recover, or two histories disagree.

This page reproduces the errors you will meet most often in throwaway repositories, explains what each one is protecting you from, and lists the safe fix. Message wording varies slightly between Git versions, so match on the meaning rather than the exact text.

Terminal
git status          # first stop: where am I, what is changed?
git log --oneline --graph --all -10
git reflog          # where HEAD has been, if something looks lost

Try it

Bash Wrong place and nothing to commit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Pushes that get rejected

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Dirty tree, conflicts and detached HEAD

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Reading an error

Every message follows a rough shape: fatal: (the command stopped), error: (something failed, the command may continue), warning: or hint: (advice). The hint: lines are written by the Git developers for exactly the situation you are in, so read them before searching the web. You can silence a hint for good with git config --global advice.<name> false once you know it by heart.

Exit codes follow the same split: 128 is a fatal error such as a missing repository or bad revision, 1 is a command-specific failure such as a conflict or "nothing to commit", and 0 is success. Scripts should check these rather than parse text.

The errors, grouped by cause

You are in the wrong place

  • fatal: not a git repository (or any of the parent directories): .git (newer builds may say "or any parent up to mount point /"): there is no .git here or in any parent. Change into your project, or run git init / git clone. If the folder used to work, check that .git was not deleted.
  • fatal: detected dubious ownership in repository at '...': the folder belongs to another user, and Git refuses to read its config for safety. If you trust it, run git config --global --add safe.directory <path>. See safe.directory.
  • fatal: Not a valid object name main or ambiguous argument 'HEAD': unknown revision: the repository has no commits yet, or the name does not exist. Make a first commit, or check names with git branch -a.

Nothing to do, or the wrong thing named

  • nothing to commit, working tree clean / no changes added to commit: you have not staged anything (see the staging area). Use git add, or git commit -a for tracked files.
  • pathspec 'x' did not match any files: the path does not exist, is ignored, or the case is wrong. git add -f overrides ignore rules; see .gitignore.
  • error: pathspec 'x' did not match any file(s) known to git: git checkout x or git switch x found no such branch or file. Fetch first if the branch lives on a remote.

Git refuses to lose your work

  • error: Your local changes to the following files would be overwritten by checkout: switching branches would clobber uncommitted edits. Commit them, or git stash, then switch.
  • error: Your local changes ... would be overwritten by merge: same protection for merge and pull.
  • error: The following untracked working tree files would be overwritten: an untracked file has the same name as one in the branch you are moving to. Move or delete it.
  • fatal: refusing to merge unrelated histories: the two branches share no common ancestor, typical when joining two separately created repositories. If that is intended, add --allow-unrelated-histories.

Two histories disagree

  • ! [rejected] main -> main (fetch first) or (non-fast-forward): the remote has commits you lack, so a push would erase them. Integrate first with git pull --rebase (or git pull with a merge), then push. Avoid --force; if you must overwrite, use --force-with-lease, which fails if the remote moved again.
  • fatal: Need to specify how to reconcile divergent branches: both sides have new commits and no pull.rebase or pull.ff setting tells Git what to do. Pass --rebase, --no-rebase or --ff-only once, or set git config --global pull.rebase true. See pull settings.
  • CONFLICT (content): Merge conflict in f.txt / Automatic merge failed; fix conflicts and then commit the result: both sides changed the same lines. Edit the file to remove the <<<<<<<, =======, >>>>>>> markers, git add it, then git commit (merge) or git rebase --continue. To give up, run git merge --abort or git rebase --abort.
  • fatal: You have not concluded your merge (MERGE_HEAD exists): a previous merge is still open. Finish or abort it.

Remotes and authentication

  • fatal: 'origin' does not appear to be a git repository: no remote of that name, or the URL is wrong. Check git remote -v.
  • remote: Permission denied / Authentication failed: wrong credentials or no write access. Fix the credential helper or SSH key rather than retrying.
  • fatal: unable to access '...': Could not resolve host: a network or proxy problem, not a Git problem.
  • error: failed to push some refs: only the closing line of a longer message. Read the lines above it for the real cause.

HEAD and branches

  • You are in 'detached HEAD' state: HEAD points at a commit, not a branch, so new commits belong to no branch and become hard to find. Run git switch -c new-branch to keep them, or git switch - to leave. Details in detached HEAD.
  • error: Cannot delete branch 'x' checked out at '...': you are on that branch, or another worktree is. Switch away first.
  • error: The branch 'x' is not fully merged: git branch -d protects unmerged work. Use -D only after confirming with git log main..x.

A file that will not go away or stay ignored

  • warning: LF will be replaced by CRLF: line-ending conversion notice from core.autocrlf; harmless but noisy.
  • error: unable to create file ...: Filename too long (Windows): set git config --global core.longpaths true.
  • fatal: unable to auto-detect email address: set user.name and user.email; see user settings.
  • fatal: bad object / error: object file ... is empty: repository corruption, often from a crash or disk problem. Try git fsck, then re-clone and carry over unpushed work from the damaged copy.

Usage notes

  • Fix the cause, not the symptom: forcing a push or deleting a lock file is the last resort, not the first.
  • fatal: Unable to create '.git/index.lock': File exists means another Git process is running or one crashed. Check that none is running, then delete the lock file.
  • If a command goes wrong halfway, git status tells you which operation is in progress (merge, rebase, cherry-pick, bisect) and how to continue or abort it.
  • Almost nothing you committed is truly gone; see git reflog before you assume the worst.
  • Search the exact first line of the message, without your paths and names, if you need outside help.