Git Notes
Every developer encounters Git problems — from confusing error messages to accidentally lost work. This guide covers the most common issues you will face,...
Every developer encounters Git problems — from confusing error messages to accidentally lost work. This guide covers the most common issues you will face, their root causes, and step-by-step solutions. Bookmark this page for those moments when Git seems to be working against you rather than for you.
Permission and Authentication Errors
Permission denied (publickey)
$ git push origin main
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.Cause: SSH key is not configured or not added to your GitHub account.
Fix:
# Check if you have an SSH key
ls ~/.ssh/id_ed25519.pub
# If not, generate one
ssh-keygen -t ed25519 -C "your@email.com"
# Add to SSH agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
# Copy public key and add to GitHub Settings → SSH Keys
cat ~/.ssh/id_ed25519.pub
# Test connection
ssh -T git@github.comAuthentication failed (HTTPS)
remote: Support for password authentication was removed.
fatal: Authentication failedFix: Use a Personal Access Token instead of password:
# Generate token at GitHub → Settings → Developer settings → Personal access tokens
# Use token as password when prompted, or configure credential helper:
git config --global credential.helper storeMerge and Conflict Issues
Merge conflict in file
CONFLICT (content): Merge conflict in src/app.js
Automatic merge failed; fix conflicts and then commit the result.Fix:
# See which files are conflicted
git status
# Open each conflicted file, look for markers:
# <<<<<<< HEAD
# (your changes)
# =======
# (their changes)
# >>>>>>>
# Edit the file to resolve, then:
git add src/app.js
git commit -m "Resolve merge conflict in app.js"Cannot merge — uncommitted changes
error: Your local changes would be overwritten by merge.Fix:
git stash # Save changes temporarily
git merge main # Now merge succeeds
git stash pop # Restore your changesReset and Recovery
Accidentally deleted commits with git reset
# Find lost commits in reflog
git reflog
# abc1234 HEAD@{2}: commit: The commit you lost
# Recover it
git checkout -b recovery abc1234
# Or reset back to it
git reset --hard abc1234Committed to wrong branch
# If not pushed yet:
git checkout correct-branch
git cherry-pick wrong-branch # Copy the commit
git checkout wrong-branch
git reset --hard HEAD~1 # Remove from wrong branchWant to undo the last commit but keep changes
git reset --soft HEAD~1 # Uncommit, keep changes staged
git reset --mixed HEAD~1 # Uncommit, keep changes unstaged
git reset --hard HEAD~1 # Uncommit and DELETE changes (careful!)Push and Pull Issues
Updates were rejected (non-fast-forward)
Cause: Remote has commits you do not have locally.
Fix:
git pull origin main # Get remote changes first
git push origin main # Now push succeeds
# Or with rebase:
git pull --rebase origin main
git push origin mainDetached HEAD state
You are in 'detached HEAD' state.Cause: You checked out a specific commit instead of a branch.
Fix:
# If you want to keep work done in detached state:
git checkout -b save-my-work
# If you just want to go back to a branch:
git checkout mainFile and Staging Issues
File too large to push
remote: error: File large-file.zip is 150 MB; exceeding GitHub's 100 MB limit.Fix:
# Remove from current commit
git rm --cached large-file.zip
echo "large-file.zip" >> .gitignore
git commit --amend
# If already in history, remove from all commits:
git filter-branch --tree-filter 'rm -f large-file.zip' HEAD
# Consider using Git LFS for large filesAccidentally staged files
git restore --staged file.txt # Unstage specific file
git restore --staged . # Unstage everythingGitignore not working for already tracked files
# Remove from Git tracking (keeps local file)
git rm --cached file-to-ignore.txt
git commit -m "Remove file from tracking"
# Now .gitignore will work for this fileConfiguration Issues
Line ending problems (Windows/Mac/Linux)
# Configure for your OS:
git config --global core.autocrlf true # Windows
git config --global core.autocrlf input # Mac/LinuxWrong author on commits
# Fix for future commits
git config user.name "Correct Name"
git config user.email "correct@email.com"
# Fix last commit author
git commit --amend --author="Correct Name <correct@email.com>"Key Takeaways
Most Git problems have straightforward solutions once you understand what caused them. Keep the reflog in mind as your safety net — it preserves history even after destructive operations. When in doubt, git status tells you where you are, and git stash gives you a clean slate to work from. The more you troubleshoot, the more confident you become, and the less these issues disrupt your workflow.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Git Troubleshooting Guide.
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, interview, preparation, troubleshooting, guide
Related Git & GitHub Topics