Git Notes
Quick reference for common Git error messages and their solutions. Includes push rejections, merge conflicts, authentication errors, and more.
Every developer encounters Git errors. The cryptic error messages can be intimidating at first, but once you learn to read them, they usually tell you exactly what went wrong and hint at the solution. This guide covers the errors you will see most frequently, with copy-paste solutions and explanations of what caused each problem.
Authentication Errors
Permission Denied (publickey)
git@github.com: Permission denied (publickey). fatal: Could not read from remote repository.
Cause: Your SSH key is not configured or not loaded into the SSH agent.
# Step 1: Check if you have an SSH key
ls ~/.ssh/id_ed25519.pub # or id_rsa.pub
# Step 2: If no key exists, generate one
ssh-keygen -t ed25519 -C "your-email@example.com"
# Step 3: Add key to SSH agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
# Step 4: Add public key to GitHub
cat ~/.ssh/id_ed25519.pub # Copy and add to GitHub Settings
# Step 5: Test connection
ssh -T git@github.com
# Alternative: Switch to HTTPS
git remote set-url origin https://github.com/user/repo.gitSupport for Password Authentication Was Removed
remote: Support for password authentication was removed on August 13, 2021. fatal: Authentication failed for 'https://github.com/...'
Cause: GitHub no longer accepts passwords. You need a token or SSH key.
# Option 1: Use a Personal Access Token
# Generate at: GitHub → Settings → Developer settings → Personal access tokens
git remote set-url origin https://ghp_YOUR_TOKEN@github.com/user/repo.git
# Option 2: Switch to SSH
git remote set-url origin git@github.com:user/repo.git
# Option 3: Use GitHub CLI
gh auth loginPush and Pull Errors
Push Rejected (Non-Fast-Forward)
! [rejected] main -> main (non-fast-forward) error: failed to push some refs to 'github.com:user/repo.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart.
Cause: Someone else pushed commits that you do not have locally.
# Solution 1: Pull with rebase (preferred - clean history)
git pull --rebase origin main
git push origin main
# Solution 2: Pull with merge
git pull origin main
# Resolve any conflicts if needed
git push origin main
# WRONG: Do NOT force push to shared branches
# git push --force ← This destroys others' work!Your Local Changes Would Be Overwritten by Merge
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.Cause: You have uncommitted changes that conflict with the incoming changes.
# Option 1: Stash your changes, pull, then restore
git stash
git pull origin main
git stash pop
# Option 2: Commit your changes first, then pull
git add .
git commit -m "WIP: save current progress"
git pull --rebase origin main
# Option 3: Discard your local changes (if not needed)
git checkout -- src/app.js
git pull origin mainFailed to Push Some Refs (Diverged)
! [rejected] main -> main (fetch first) hint: Updates were rejected because the remote contains work that you do not have locally.
Cause: Both you and the remote have new commits since your last sync.
# Always fetch first to see the situation
git fetch origin
git log --oneline HEAD..origin/main # What they have that you don't
# Then integrate and push
git pull --rebase origin main
git push origin mainBranch Errors
Branch Already Exists
fatal: A branch named 'feature/auth' already exists.
# Switch to the existing branch
git checkout feature/auth
# Or create with a different name
git checkout -b feature/auth-v2
# Or delete the old one and recreate
git branch -D feature/auth
git checkout -b feature/authCannot Delete Branch Checked Out
error: Cannot delete branch 'feature/auth' checked out at '/path/to/repo'
# Switch to a different branch first
git checkout main
git branch -d feature/authDetached HEAD State
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.
Cause: You checked out a specific commit hash instead of a branch.
# If you want to keep work done in detached state:
git checkout -b save-my-work
# If you just want to get back to a branch:
git checkout main
# If you accidentally checked out a tag:
git checkout mainMerge and Rebase Errors
Merge Conflict
Auto-merging src/config.js CONFLICT (content): Merge conflict in src/config.js Automatic merge failed; fix conflicts and then commit the result.
# Step 1: See all conflicted files
git status
# Step 2: Open each file and resolve conflict markers
# Look for: <<<<<<< HEAD, =======, >>>>>>> branch-name
code src/config.js # Edit and choose correct content
# Step 3: Stage resolved files
git add src/config.js
# Step 4: Complete the merge
git commit -m "merge: resolve config conflict"
# Or abort if you want to start over
git merge --abortRebase Conflict
CONFLICT (content): Merge conflict in src/api.js error: could not apply abc1234... feat: add endpoint
# Resolve the conflict in the file
code src/api.js
# Stage and continue rebase
git add src/api.js
git rebase --continue
# Or abort the entire rebase
git rebase --abortFile Size and History Errors
File Exceeds GitHub Size Limit
remote: error: File data/backup.sql is 156.78 MB; this exceeds GitHub's file size limit of 100.00 MB
# Remove from current commit
git rm --cached data/backup.sql
echo "data/backup.sql" >> .gitignore
git commit -m "remove large file, add to gitignore"
# If already in history, remove from ALL commits
git filter-repo --invert-paths --path data/backup.sql
git push --force-with-lease
# For legitimate large files, use Git LFS
git lfs install
git lfs track "*.sql"
git add .gitattributesConfiguration Errors
Author Identity Unknown
Author identity unknown
*** Please tell me who you are.
Run: git config --global user.email "you@example.com"
git config --global user.name "Your Name"git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"Interview Questions
Q1: What is the most common Git error you encounter and how do you fix it?
Push rejection because the remote has newer commits. Fixed by git pull --rebase origin main then git push. This happens naturally in team environments and is resolved by integrating remote changes before pushing.
Q2: How do you fix detached HEAD state?
If you want to keep commits made in detached state, create a branch with git checkout -b branch-name. If you do not need those commits, simply git checkout main to return to a branch. Detached HEAD happens when checking out a specific commit or tag instead of a branch.
Q3: What do you do when you accidentally commit sensitive data?
Immediately revoke the credential (assume compromise), remove from the entire Git history using git filter-repo or BFG Repo-Cleaner, force push the cleaned history, add the file to .gitignore, and implement prevention measures like pre-commit hooks.
Q4: How do you resolve a merge conflict?
Open the conflicted file, find the conflict markers (<<<, ===, >>>), understand both versions, choose the correct content (or combine them), remove all markers, save, git add the resolved file, and complete with git commit or git rebase --continue.
Q5: What if git pull says your changes would be overwritten?
You have uncommitted modifications conflicting with incoming changes. Either commit your work first (git add . && git commit), stash it (git stash && git pull && git stash pop), or discard it (git restore .). Never force your way past this error.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Common Git Errors and Fixes - Quick Solutions.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, resources, common, errors, and
Related Git & GitHub Topics