Class 50 of 50 • Practice & Viva Prep

Git & GitHub Troubleshooting Guide: 50+ Common Fatal Errors and Fixes

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.

1. Error: fatal: refusing to merge unrelated histories

Cause: 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

2. Error: [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

3. Error: 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

4. Error: error: Your local changes to the following files would be overwritten by checkout

Cause: 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

5. Issue: Accidentally Committed Huge File (>100MB) and Push is Blocked

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

6. Issue: Accidentally Committed Passwords, API Keys or .env

Immediate Emergency Actions:

  1. Rotate the leaked credentials immediately! Assume the secret is compromised. Change the API key in your cloud dashboard.
  2. Remove the file from Git tracking: git rm --cached .env.
  3. Add .env to .gitignore.
  4. Commit the fix: git commit -m "security: untrack .env credentials".
  5. To completely purge the secret from past historical commit objects, use the open-source git-filter-repo tool:
    git filter-repo --path .env --invert-paths

7. Issue: "You are in 'detached HEAD' state"

# 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

Explore Related Git & GitHub Tutorials & Next Steps

Strengthen your version control mastery by studying these connected classes:

Recovering Lost Work with git reflog

The ultimate recovery tool for locating abandoned commits and deleted branches.

Resolving Complex Merge Conflicts

Detailed step-by-step walkthrough for untangling difficult three-way merges.

30+ Hands-on Practice Scenarios

Simulate errors in isolated test repositories before touching production code.

120+ Top Interview & Viva Questions

Master technical explanations for common Git gotchas and edge cases.

Frequently Asked Questions (FAQ) - Student & Interview Review

Press `q` (if Git is displaying output in the `less` pager), or press `Ctrl + C` to interrupt and kill the running process.
Run `git fsck` (File System Check). Git will verify the integrity of all SHA-1 hashes and report any dangling or corrupted objects.
Once pushed to GitHub, you cannot truly 'undo' a push if teammates have pulled it. The standard safe solution is to run `git revert [commit-hash]` and push the revert commit.

Next Step in Your Git & GitHub Learning Path:

Advance your software engineering skills with the next topic in our synchronized curriculum.

Continue to Home & Syllabus → Previous: ← CLI Cheat Sheet | Full Syllabus

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!