Skip to main content

Git Troubleshooting Guide

This guide covers common Git issues you might encounter when working with GitHub repositories, with detailed solutions and best practices to prevent future problems.

Divergent Branches Error

One of the most common issues developers face is the "divergent branches" error when trying to pull from a remote repository.

Understanding the Error

When you see this error message:

hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint: git config pull.rebase false # merge
hint: git config pull.rebase true # rebase
hint: git config pull.ff only # fast-forward only

This happens when:

  • Your local branch has commits that aren't on the remote branch
  • The remote branch has commits that aren't on your local branch
  • Git doesn't know how you want to combine these divergent histories

Quick Fix for the Error:

If you're on the wrong branch (common cause):

# Check which branch you're on
git branch

# Switch to main branch
git checkout main

# Now pull should work
git pull origin main

If you're on the correct branch:

# Option 1: Use merge strategy (preserves branch history)
git config pull.rebase false
git pull

# Option 2: Use rebase strategy (creates linear history)
git config pull.rebase true
git pull

# Option 3: Use fast-forward only (fails if not possible)
git config pull.ff only
git pull

Common Git Issues and Solutions

Problem: Authentication failed when pushing/pulling

Symptoms:

remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/...'

Solutions:

1. Use Personal Access Token (Recommended):

# Generate token at: https://github.com/settings/tokens
# Use token as password when prompted
git push origin main

2. Use GitHub CLI:

# Install GitHub CLI: https://cli.github.com/
gh auth login
# Follow prompts to authenticate

3. Set Up SSH Keys:

# Generate SSH key
ssh-keygen -t ed25519 -C "[email protected]"

# Add to SSH agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

# Add public key to GitHub account
cat ~/.ssh/id_ed25519.pub
# Copy output and add to GitHub Settings > SSH Keys

# Test connection
ssh -T [email protected]

4. Update Remote URL to Use SSH:

# Change from HTTPS to SSH
git remote set-url origin [email protected]:username/repository.git

Advanced Troubleshooting

Fixing Commit History Issues:

Undo Last Commit (Keep Changes):

git reset --soft HEAD~1

Undo Last Commit (Discard Changes):

git reset --hard HEAD~1

Amend Last Commit:

# Change commit message
git commit --amend -m "New message"

# Add files to last commit
git add forgotten-file
git commit --amend --no-edit

Revert a Commit (Safe for Shared History):

git revert commit-hash

Interactive Rebase (Rewrite History):

# Rebase last 3 commits
git rebase -i HEAD~3
# Use only on commits not pushed to shared repositories

Git Configuration Best Practices

Essential Global Git Settings:

# User identity
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

# Default branch name
git config --global init.defaultBranch main

# Pull strategy (choose one)
git config --global pull.rebase false # merge
# git config --global pull.rebase true # rebase
# git config --global pull.ff only # fast-forward only

# Push behavior
git config --global push.default simple

# Editor for commit messages
git config --global core.editor "code --wait" # VS Code
# git config --global core.editor "vim" # Vim

# Diff and merge tools
git config --global diff.tool vscode
git config --global merge.tool vscode

# Auto-correct typos
git config --global help.autocorrect 1

# Better diff algorithm
git config --global diff.algorithm patience

Emergency Git Commands

When things go wrong, these commands can help recover:

# See what you did recently
git reflog

# Undo almost anything
git reset --hard HEAD~1

# Save work in progress
git stash

# Recover deleted branch
git checkout -b recovered-branch commit-hash

# Force push (use with extreme caution)
git push --force-with-lease

# Start over completely
git reset --hard origin/main
caution

Warning: Commands like reset --hard and push --force can permanently delete work. Always make sure you have backups or that the work isn't needed before using destructive commands.

Buy me a beer


Buy me a coffee