Common Git errors trip up almost every developer at some point, from total beginners to people who have used Git for years. One minute your commit goes through fine, the next you’re staring at a red error message that makes no sense. The good news is that almost every Git error has a simple, repeatable fix once you understand what’s actually going on.
In this guide, we’ll break down the 10 Git errors developers run into the most, explain why they happen, and show you exactly how to fix each one. No jargon overload, just clear steps you can follow right now.
Why Git Errors Happen So Often
Git is powerful, but that power comes with a learning curve. Most errors come down to three root causes:
- Wrong identity or permissions — you’re pushing with the wrong account or lack access.
- Out-of-sync history — your local branch and the remote branch have drifted apart.
- Accidental actions — committing the wrong file, forcing a push, or checking out the wrong reference.
Once you recognize which bucket an error falls into, fixing it becomes a lot less scary.
1. “fatal: not a git repository”
This is one of the first errors new users see, and it simply means you’re running a Git command outside of a folder that Git is tracking.
Why it happens:
- You haven’t run
git inityet - You’re in the wrong folder entirely
How to fix it:
- Check your current directory with
pwd(orcdon Windows). - Navigate to the correct project folder.
- If it truly isn’t a Git repo yet, run
git initto start tracking it.
2. “failed to push some refs” (non-fast-forward)
You’ll usually see this alongside ! [rejected] main -> main (non-fast-forward). It means someone else pushed commits to the remote branch after your last pull.
Why it happens:
- A teammate pushed changes before you did
- You pushed from a different machine and forgot to sync
How to fix it:
- Run
git fetch originto grab the latest changes. - Merge with
git pull --no-rebase origin main, or replay your work on top withgit pull --rebase origin main. - Resolve any conflicts that come up, then push again with
git push origin main.
Avoid using --force here unless you are absolutely sure you want to overwrite someone else’s commits. If you must force a push, use --force-with-lease instead, since it checks that no one else has pushed before overwriting.
3. Merge Conflicts
A merge conflict shows up when Git can’t automatically combine two sets of changes that touch the same lines in a file. You’ll see a message like “Automatic merge failed; fix conflicts and then commit the result.”
How to fix it:
- Open the file Git flags as conflicted.
- Look for the conflict markers
<<<<<<<,=======, and>>>>>>>. - Edit the file to keep the version you want (or a combination of both).
- Remove the conflict markers completely.
- Stage the fixed file with
git add, then finish withgit commit.
4. Detached HEAD State
This happens when you check out a specific commit hash instead of a branch name. Git warns you that you’re in a “detached HEAD” state, meaning new commits won’t belong to any branch.
Why it matters: Any commits you make here can get lost once you switch back to a branch, since nothing points to them anymore.
How to fix it:
- If you want to keep your changes, create a new branch right away:
git checkout -b new-branch-name - If you don’t need the changes, simply check out your usual branch, like
git checkout main
5. “Permission denied (publickey)” or Push Access Errors
This error appears when Git is trying to push using the wrong account, an expired token, or a missing SSH key.
Common causes:
- You’re logged into multiple GitHub accounts
- Your SSH key isn’t linked to the account with repo access
- Cached HTTPS credentials are pointing to the wrong user
How to fix it:
- For HTTPS, clear cached credentials from your system’s keychain and log in again.
- For SSH, confirm the correct key is added to your GitHub account settings.
- Double-check that your account actually has access to the repository.
6. Large File Rejected by GitHub
If you try to push a file over 100MB, GitHub blocks it with an error like GH001: Large files detected.
Typical offenders:
- Trained machine learning models
- Video files or large datasets
- Accidentally committed
node_modules/orvenv/folders
How to fix it:
- Remove the large file from your latest commit if it hasn’t been pushed yet.
- Add the file type to
.gitignoreso it doesn’t get tracked again. - If the file is already buried in your history, use
git filter-repoto remove it, or switch to Git LFS (Large File Storage) for files you genuinely need to keep in the repo.
7. “fatal: refusing to merge unrelated histories”
This shows up when you try to merge two projects that never shared a common commit history, like connecting a fresh local repo to an existing remote one.
How to fix it: Run the following command, swapping in your actual branch name:
git pull origin main --allow-unrelated-historiesThis flag tells Git it’s okay to merge the two histories together. If there are conflicting files, resolve them the same way you would with any other merge conflict.
8. Remote Origin Already Exists
You’ll see fatal: remote origin already exists when you try to add a remote named “origin,” but one is already set up in your local repository.
How to fix it:
- Option 1: Update the existing URL instead of adding a new one:
git remote set-url origin https://github.com/username/repo.git- Option 2: Remove the old remote first, then add the new one:
git remote remove origin
git remote add origin https://github.com/username/repo.git9. “pathspec did not match any file(s) known to git”
This common Git error means Git can’t find the branch, file, or path you referenced in your command.
Common causes:
- Typos in the branch or file name
- Trying to check out a remote branch that hasn’t been fetched yet
- Case sensitivity issues between operating systems
How to fix it:
- Run
git branch -ato see all local and remote branches. - If the branch only exists remotely, fetch it first with
git fetch --all, then check it out. - Use
git ls-filesto confirm the exact file name and path before trying again.
10. Losing Commits or a “Broken” Repository
Sometimes it feels like your commits vanished after a reset, rebase, or bad merge. The good news is Git rarely deletes anything permanently right away.
How to fix it:
- Run
git reflogto see a full history of everything you’ve done, across all branches. - Find the entry from before things went wrong, noted as
HEAD@{index}. - Run
git reset HEAD@{index}to jump back to that point.
The reflog has saved countless developers from what looked like permanent data loss, so don’t panic if a commit seems to disappear.
How to Avoid These Git Errors in the First Place
A few small habits go a long way toward preventing most of these problems before they start:
- Set up a proper
.gitignorefile before your very first commit. - Pull or fetch frequently so your local branch doesn’t drift too far from the remote.
- Avoid plain
--forcepushes on shared branches; use--force-with-leaseif you must force a push. - Double-check which branch you’re on before committing, especially after checking out a specific commit.
- Use Git LFS for large binary files like models, videos, or datasets from the start.
Frequently Asked Questions
What is the most common Git error for beginners?
“fatal: not a git repository” is one of the most common Git errors beginners run into, usually because they’re running a command in the wrong folder or haven’t initialized Git yet.
How do I fix a merge conflict in Git?
Open the conflicted file, look for the conflict markers, manually choose which changes to keep, remove the markers, then stage and commit the resolved file.
Is it safe to use git push –force?
It can overwrite a teammate’s commits without warning. It’s safer to use git push --force-with-lease, which checks that no one else has pushed before your force push goes through.
How do I recover a commit I think I lost?
Run git reflog to see a history of everything you’ve done, find the point before things broke, and use git reset to return to that state.
Why does Git say my file is too large to push?
GitHub blocks any single file over 100MB. Add large files to .gitignore, or use Git LFS if you need to track them properly.
What causes “refusing to merge unrelated histories”?
This happens when you try to merge two projects that don’t share a common commit history. Adding --allow-unrelated-histories to your pull command tells Git it’s fine to combine them anyway.
Final Thoughts
Common Git errors look intimidating at first, but almost every one of them follows a predictable pattern once you know what to look for. Whether it’s a rejected push, a merge conflict, or a repository that feels completely broken, Git almost always gives you a way back with tools like reflog, fetch, and reset.
Keep this guide handy the next time a red error message pops up in your terminal. With a bit of practice, these fixes will become second nature.
Editor’s Opinion
I’ve hit like half of these erors myself, mostly the merge conflict one and that scary detached HEAD message. Honestly the biggest lesson is just dont panic, git almost never actually deletes your work, reflog is basically a life saver. If your new to git just go slow, read the error message properly instead of googling right away, sometimes it already tells you the answer. Practice makes it way less scary over time, trust me.