Git Error Fixes -- How to Fix Common Git Errors
Git errors like merge conflicts and detached HEAD stop your workflow cold -- this guide shows you how to diagnose and fix common Git errors with exact commands, output examples, and recovery strategies.
What You'll Learn
Why It Matters
Git version control is the backbone of modern software development. A single Git error can block deployments, lose commits, or corrupt your repository if handled incorrectly.
Real-World Use
When a teammate pushes force-pushed changes or a merge conflict blocks your pull request, knowing how to resolve Git errors confidently keeps your team productive and your codebase stable.
Common Git Errors Table
| Error Message | Cause | Fix |
|---|---|---|
| fatal: not a git repository | Directory not initialized with Git | Run git init or clone the repo first |
| CONFLICT (content): Merge conflict in file | Two branches modified the same file lines | Edit the file, resolve markers, then git add and commit |
| fatal: refusing to merge unrelated histories | Branches have no common commit ancestor | Use git merge --allow-unrelated-histories or rebase |
| error: failed to push some refs | Remote has commits you do not have locally | Pull remote changes with git pull --rebase first |
| You are in detached HEAD state | Checked out a commit hash instead of a branch | Create a branch with git switch -c <branch> |
| fatal: The current branch has no upstream branch | No remote tracking branch configured | Push with git push -u origin <branch> |
| error: Your local changes would be overwritten | Switching branches with uncommitted changes | Stash with git stash before switching |
Step-by-Step Fixes
Fix 1: Merge Conflict Resolution
# bad -- conflict markers appear in the file
cat index.html
# <<<<<<< HEAD
# <title>Site Title</title>
# =======
# <title>New Title</title>
# >>>>>>> feature-branch
# fixed -- edit the file to keep the correct version
cat index.html
# <title>New Title</title>
# Stage and commit the resolved file
git add index.html
git commit -m "Resolve merge conflict in index.html"
Expected output:
[main d4f5e2b] Resolve merge conflict in index.html
Fix 2: Detached HEAD Recovery
# bad -- checked out a commit hash
git checkout 4a2b8c1
# You are in 'detached HEAD' state.
# fixed -- create a branch from the current commit
git switch -c recovery-branch
# Switched to a new branch 'recovery-branch'
# Now you can merge or rebase safely
Expected output:
Switched to a new branch 'recovery-branch'
Fix 3: Push Rejected (Non-Fast-Forward)
# bad
git push origin main
# ! [rejected] main -> main (non-fast-forward)
# error: failed to push some refs
# fixed -- pull remote changes first
git pull --rebase origin main
# Resolve any conflicts if they appear
git push origin main
Expected output:
Enumerating objects: 5, done.
Writing objects: 100% (3/3), 320 bytes | 320.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To github.com:user/repo.git
a1b2c3d..e4f5g6h main -> main
Fix 4: Unstaged Changes Blocking Branch Switch
# bad
git checkout develop
# error: Your local changes would be overwritten by checkout
# fixed -- stash changes, switch, then unstash
git stash
git checkout develop
git stash pop
Expected output:
Saved working directory and index state WIP on main: a1b2c3d Commit message
Switched to branch 'develop'
Dropped refs/stash@{0} (f2e3d4c5...)
Fix 5: No Upstream Branch
# bad
git push
# fatal: The current branch feature-x has no upstream branch
# fixed -- set upstream on first push
git push -u origin feature-x
Expected output:
Enumerating objects: 4, done.
* [new branch] feature-x -> feature-x
branch 'feature-x' set up to track 'origin/feature-x'.
Git Error Recovery Flowchart
flowchart TD
A[Git Error Occurs] --> B{Error Type?}
B -->|Merge Conflict| C[Edit conflicted file]
C --> D[Remove conflict markers]
D --> E[git add and commit]
B -->|Push Rejected| F[git pull --rebase]
F --> G[Fix conflicts if any]
G --> H[git push]
B -->|Detached HEAD| I[git switch -c new-branch]
I --> J[Branch created safely]
B -->|Changes Would Overwrite| K[git stash]
K --> L[Switch branches]
L --> M[git stash pop]
E --> N[Git Working]
H --> N
J --> N
M --> N
Prevention Tips
- Always pull before you push to avoid non-fast-forward rejections
- Use
git pull --rebaseinstead of merge pull for cleaner history - Commit or stash changes before switching branches
- Set up branch protection rules on GitHub/GitLab to prevent force pushes to main
- Use descriptive commit messages to make conflict resolution easier
- Configure
git config --global pull.rebase truefor default rebase pulls - Create feature branches from the latest main to reduce merge conflicts
Practice Questions
What does a merge conflict look like in a file and how do you resolve it? Answer: Conflict markers
<<<<<<< HEADto=======and>>>>>>> branchappear. Edit the file to keep the desired code, remove the markers, thengit addand commit.How do you recover commits made in detached HEAD state? Answer: Create a branch with
git switch -c <branch-name>immediately. This preserves all commits made while detached.What causes a non-fast-forward push rejection? Answer: The remote branch has commits you do not have locally. Pull the remote changes first with
git pull --rebasethen push.How do you save uncommitted work temporarily and restore it later? Answer: Use
git stashto save changes, thengit stash popto restore them after switching branches.Challenge: Write a Git alias or shell script that automates the process of stashing, switching to main, pulling latest, switching back, and unstashing -- all in one command. Answer:
# Add to ~/.gitconfig as alias # [alias] # sync = "!f() { git stash && git switch main && git pull && git switch - && git stash pop; }; f" # Or as a shell function git_sync() { local branch=$(git branch --show-current) git stash git switch main git pull --rebase git switch "$branch" git stash pop }
Quick Reference
| Error | Cause | Quick Fix |
|---|---|---|
| Not a git repository | No .git directory |
git init or git clone <url> |
| Merge conflict | Same file edited on two branches | Edit file, git add, git commit |
| Push rejected | Remote ahead of local | git pull --rebase then push |
| Detached HEAD | Checked out a commit hash | git switch -c <branch> |
| Changes overwritten | Uncommitted changes on branch switch | git stash then git stash pop |
| No upstream branch | No remote tracking set | git push -u origin <branch> |
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro