Git Reference
Git Reference / Common problems

19Common problems

The error messages you’ll actually hit, what causes them, and how to fix them. Search for words from your error to jump straight to it.

Pushing & pulling

Push rejected: remote has new work

! [rejected] main -> main (fetch first)
Updates were rejected because the remote contains work that you do not have locally.

Why: someone pushed since your last pull. Integrate their commits first, then push again.

git pull --rebase
git push

No upstream branch

fatal: The current branch feature has no upstream branch.

Why: a new local branch doesn’t know which remote branch to push to yet.

git push -u origin HEAD
# never see it again:
git config --global push.autoSetupRemote true

Divergent branches on pull

hint: You have divergent branches and need to specify how to reconcile them.

Why: both you and the remote have new commits, and Git wants you to choose merge or rebase.

git pull --rebase        # linear history
git pull --no-rebase     # merge commit
git config --global pull.rebase true   # set a default

Refusing to merge unrelated histories

fatal: refusing to merge unrelated histories

Why: you ran git init locally and the GitHub repo was created with its own README/license commit. The two have no common ancestor.

git pull origin main --allow-unrelated-histories

src refspec does not match any

error: src refspec main does not match any

Why: there are no commits yet, or your branch is called master, not main.

git branch --show-current   # what is it called?
git commit -m "Initial commit"  # if nothing committed
git branch -M main
git push -u origin main

File too large for GitHub

remote: error: File big.zip is 142 MB; this exceeds GitHub's file size limit of 100.00 MB

Why: the file is in a commit, so deleting it in a new commit isn’t enough. Remove it from history.

# in the latest commit:
git rm --cached big.zip && git commit --amend --no-edit
# in older commits:
git filter-repo --path big.zip --invert-paths
# or move it to LFS:
git lfs migrate import --include="*.zip"

Authentication

Password authentication removed

remote: Support for password authentication was removed.
fatal: Authentication failed for 'https://github.com/…'

Why: GitHub no longer accepts account passwords over HTTPS. Use the GitHub CLI, a personal access token, or SSH.

gh auth login
gh auth setup-git      # use gh as credential helper

Permission denied (publickey)

git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Why: no SSH key is loaded, or the key isn’t added to your GitHub account.

ssh -T git@github.com            # test the connection
ssh-add -l                       # loaded keys
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
git remote -v                    # ssh or https URL?

Repository not found

remote: Repository not found.
fatal: repository 'https://github.com/…' not found

Why: a typo in the URL, or the repo is private and you’re signed in as an account without access.

git remote -v
gh auth status
git remote set-url origin git@github.com:owner/repo.git

Branches & commits

Committed to the wrong branch

You made commits on main that belong on a new feature branch, and haven’t pushed them.

git branch feature            # save commits on a new branch
git reset --hard origin/main  # put main back
git switch feature

Commit belongs on an existing branch instead?

git switch right-branch
git cherry-pick <sha>
git switch -  && git reset --hard HEAD~1

Local changes would be overwritten

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

Why: you have uncommitted edits to files the other branch changes. Put them somewhere safe first.

git stash -u && git switch other && git stash pop
# or bring the changes along / commit them:
git switch -c new-branch

“You are in detached HEAD state”

You are in 'detached HEAD' state. You can look around, make experimental changes…

Why: you checked out a commit or tag rather than a branch. It’s harmless, but new commits won’t belong to any branch.

git switch -c keep-my-work   # keep commits made here
git switch main              # or just go back

Deleted a branch by accident

git branch -D prints the hash it deleted (was a1b2c3d). If that’s scrolled away, reflog has it.

git reflog | grep feature
git branch feature a1b2c3d

Dropped or lost a stash

Stashes are commits, so they linger as dangling objects until garbage collection.

git fsck --no-reflog | awk '/dangling commit/ {print $3}' \
  | xargs git log --no-walk --oneline --grep=WIP
git stash apply <sha>

Wrong name or email on commits

Fix your config, then rewrite the affected (unpushed) commits.

git config user.email "right@example.com"
git commit --amend --reset-author --no-edit   # last commit
git rebase -r main --exec "git commit --amend --reset-author --no-edit"

Undo a pushed merge on main

Don’t reset shared history. Revert it, keeping parent 1 (main) as the mainline.

git log --merges --oneline -5
git revert -m 1 <merge-sha>
git push

Need to bypass a hook

A pre-commit or pre-push hook is blocking an urgent fix. Use sparingly.

git commit --no-verify -m "Hotfix"
git push --no-verify

Stuck states

Stuck mid-merge or mid-rebase

error: You have not concluded your merge (MERGE_HEAD exists).

Why: a merge, rebase or cherry-pick stopped on a conflict. git status says which one; finish it or abort.

git status
git merge --abort        # or --continue
git rebase --abort       # or --continue / --skip
git cherry-pick --abort

index.lock exists

fatal: Unable to create '.git/index.lock': File exists.

Why: another Git process (often your editor) is running, or one crashed and left the lock behind. Make sure nothing is running, then remove it.

ps aux | grep -i git
rm .git/index.lock

Not a git repository

fatal: not a git repository (or any of the parent directories): .git

Why: you’re in the wrong folder, or the repo hasn’t been initialised.

pwd
git rev-parse --show-toplevel   # where's the repo root?
git init                        # if it's meant to be new

Editor opened and you can’t get out

Git opened Vim for a commit or merge message. Type :wq and Enter to save, or :cq to abort. Then pick a friendlier editor:

git config --global core.editor "nano"
git config --global core.editor "code --wait"

Files & diffs

.gitignore isn’t ignoring a file

Why: .gitignore only affects untracked files. If it was committed before, Git keeps tracking it.

git rm --cached .env           # or -r for a folder
git commit -m "Stop tracking .env"
git check-ignore -v .env       # confirm the rule matches

Whole file shows as changed

Why: usually line endings (CRLF vs LF) differing between Windows and macOS/Linux.

echo "* text=auto" >> .gitattributes
git add --renormalize .
git commit -m "Normalize line endings"
git config --global core.autocrlf input   # mac/linux

Only file permissions changed

old mode 100644
new mode 100755

Why: the executable bit flipped, often from copying files or using a shared drive.

git config core.fileMode false
git diff --summary | grep mode

Renaming only the case of a file

On macOS and Windows the filesystem ignores case, so readme.md → README.md can go unnoticed. Use git mv:

git mv readme.md README.md
# older Git: rename via a temporary name
git mv readme.md tmp && git mv tmp README.md

Conflict in a lock file

Don’t hand-merge package-lock.json, yarn.lock and similar. Take one side and regenerate it.

git checkout --theirs package-lock.json
npm install
git add package-lock.json

Submodule folder is empty

Why: the repo was cloned without --recurse-submodules.

git submodule update --init --recursive
git config --global submodule.recurse true