If you have been using Git for a while, you have probably run into an error that made you stop and scratch your head. Some of them look intimidating at first, but most have straightforward fixes once you know what to look for. This post covers the most common Git errors and how to resolve them.
1. "fatal: not a git repository"
This error shows up when you try to run a Git command in a folder that has not been initialized as a Git repository.
Fix: Either initialize a new repository in that folder:
git initOr make sure you are in the right directory. Use pwd to check where you are and cd to navigate to the correct folder.
2. "fatal: remote origin already exists"
This happens when you try to add a remote called origin but one has already been set for that repository.
Fix: You can update the existing remote URL instead:
git remote set-url origin https://github.com/username/repo.gitOr remove the existing remote and add a new one:
git remote remove origin
git remote add origin https://github.com/username/repo.git3. "error: failed to push some refs"
This usually means someone else has pushed changes to the remote repository since you last pulled, and your local branch is behind.
Fix: Pull the latest changes first, then push:
git pull origin main
git push origin mainIf you are confident your changes should overwrite what is on the remote, you can force push, but use this carefully as it can overwrite other people's work:
git push origin main --force4. "CONFLICT: merge conflict"
A merge conflict happens when two branches have changes to the same part of the same file and Git cannot automatically decide which one to keep.
Fix: Open the file with the conflict. Git marks the conflicting sections like this:
<<<<<<< HEAD
your changes
=======
incoming changes
>>>>>>> branch-nameManually edit the file to keep what you want, then remove the conflict markers. After that, stage the file and commit:
git add filename.txt
git commit -m "Resolve merge conflict"5. "fatal: pathspec did not match any files"
This error appears when you try to add or checkout a file that Git cannot find, usually because the filename or path is wrong.
Fix: Double check the filename and path. Use git status to see the exact names of files Git is tracking, then use the correct name in your command.
6. "error: your local changes would be overwritten by merge"
This happens when you try to pull or switch branches but you have uncommitted changes that would be overwritten.
Fix: You have two options. If you want to keep your changes temporarily, stash them:
git stash
git pull origin main
git stash popIf you do not need the changes, you can discard them:
git checkout -- .7. "detached HEAD state"
This one sounds alarming but it just means you have checked out a specific commit instead of a branch. Any commits you make in this state are not attached to any branch and can be lost.
Fix: If you want to keep the work you did, create a new branch from the current state:
git checkout -b new-branch-nameIf you just want to get back to your main branch:
git checkout main8. "fatal: refusing to merge unrelated histories"
This happens when you try to merge two repositories that do not share a common commit history, which can occur when combining a local repository with a new GitHub repository.
Fix: Allow the merge with the unrelated histories flag:
git pull origin main --allow-unrelated-histories9. Committed to the Wrong Branch
You made a commit but realize it went to the wrong branch.
Fix: First, copy the commit hash from git log. Then switch to the correct branch and cherry pick the commit:
git checkout correct-branch
git cherry-pick commit-hashGo back to the wrong branch and remove the commit:
git checkout wrong-branch
git reset HEAD~110. "fatal: unable to access" or "Could not resolve host"
This usually points to a network issue or an incorrect remote URL.
Fix: Check your internet connection first. Then verify your remote URL is correct:
git remote -vIf the URL looks wrong, update it:
git remote set-url origin https://github.com/username/repo.gitIf you are using SSH, make sure your SSH key is set up correctly and added to GitHub.
Final Thoughts
Running into Git errors is a normal part of the process. The more you use Git, the more familiar these errors become and the faster you will be able to fix them. When in doubt, git status is always a good first step. It gives you a clear picture of where things stand and often points you in the right direction.