Every developer eventually encounters a terrifying red fatal: ... error in terminal. Don't panic! Git is designed so that almost nothing committed is ever truly lost. Here is your comprehensive emergency troubleshooting handbook covering the most common Git errors and exact copy-paste solutions.
fatal: refusing to merge unrelated historiesCause: You created a local repository with git init, created a repository on GitHub with an initialized README.md, and tried to run git pull origin main. Because the two repositories share no common root commit, Git refuses to merge them to protect you.
# Solution: Force Git to merge the unrelated histories:
$ git pull origin main --allow-unrelated-histories
[rejected] error: failed to push some refs to...Cause: Another teammate (or you editing via GitHub web interface) pushed commits to origin/main that do not exist on your local computer. Git prevents you from overwriting them.
# Solution: Fetch and integrate the remote changes first, then push:
$ git pull --rebase origin main
# (Resolve any conflicts if prompted, then:)
$ git push origin main
Permission denied (publickey)Cause: You are connecting via SSH (git@github.com:...), but your local SSH agent is not running, or your public key has not been uploaded to GitHub account settings.
# Solution Step 1: Ensure SSH agent is running & add key:
$ eval "$(ssh-agent -s)"
$ ssh-add ~/.ssh/id_ed25519
# Solution Step 2: Test connection:
$ ssh -T git@github.com
# If still failing, copy your public key and add it to GitHub Settings -> SSH Keys:
$ cat ~/.ssh/id_ed25519.pub
error: Your local changes to the following files would be overwritten by checkoutCause: You have uncommitted modifications in your working directory, and the branch you want to switch to has different versions of those same files.
# Option A (Preserve work): Stash your changes, switch, and re-apply:
$ git stash
$ git switch target-branch
$ git stash pop
# Option B (Discard work): Permanently discard local edits and force switch:
$ git restore .
$ git switch target-branch
Cause: You committed a 150MB video or dataset. GitHub strictly rejects any push containing objects over 100MB, even if you delete the file in a subsequent commit, because the file remains in past commit history!
# Solution: Undo the commit containing the file while keeping your other code:
$ git reset --soft HEAD~1
$ git rm --cached heavy_file.zip
$ echo "heavy_file.zip" >> .gitignore
$ git commit -m "feat: re-commit without huge binary"
$ git push origin main
Immediate Emergency Actions:
git rm --cached .env..env to .gitignore.git commit -m "security: untrack .env credentials".git filter-repo --path .env --invert-paths
# Solution A: If you just want to return to your normal branch:
$ git switch main
# Solution B: If you made commits in detached HEAD that you want to keep:
$ git switch -c my-saved-branch
Strengthen your version control mastery by studying these connected classes:
The ultimate recovery tool for locating abandoned commits and deleted branches.
Detailed step-by-step walkthrough for untangling difficult three-way merges.
Simulate errors in isolated test repositories before touching production code.
Master technical explanations for common Git gotchas and edge cases.
Advance your software engineering skills with the next topic in our synchronized curriculum.
Continue to Home & Syllabus → Previous: ← CLI Cheat Sheet | Full Syllabus