Sub topic 13
Fixing common errors
This chapter assumes you have a local repository with at least one commit, an empty or newly created GitHub repository, and a working Git install. Most failures when connecting the two fall into four groups: the remote name is taken, the push is rejected, authentication fails, or the URL or branch name is wrong.
Read the error first
Git's messages usually name the cause. Match the first line of the message to the table before you change anything.
| Message | Likely cause | Fix |
|---|---|---|
| `error: remote origin already exists.` | A remote named `origin` is already configured | Change its URL or pick another name |
| `! [rejected] main -> main (fetch first)` | GitHub has commits you do not have | Pull, then push |
| `fatal: refusing to merge unrelated histories` | The two repositories share no commits | Pull with `--allow-unrelated-histories` |
| `error: src refspec main does not match any` | No local branch named `main`, or no commits yet | Commit, or rename the branch |
| `Permission denied (publickey)` | SSH key missing or not added to GitHub | Test with `ssh -T` |
| `Authentication failed` over HTTPS | Account password used instead of a token | Use a personal access token or `gh auth login` |
| `Repository not found` | Wrong URL, or no access to a private repository | Check the URL and your account |
An existing origin
Names are unique per repository. If git remote add origin ... fails, inspect the current setting before you decide what to do.
git remote -v
git remote set-url origin https://github.com/YOUR-USERNAME/YOUR-REPO.gitUse set-url when the existing remote points to the wrong place. If it points somewhere you still need, rename it with git remote rename origin github and add the new one.
Rejected pushes
If you created the GitHub repository with a README, it already has a commit. Your first push is then rejected because your history does not contain that commit. Pull first. Git may refuse because the histories are unrelated, and the following command merges them:
git pull origin main --allow-unrelated-histories
git push -u origin mainYou may get merge conflicts if both sides have a file such as README.md. Edit the file, run git add on it, and run git commit to finish the merge.
Branch name mismatches
error: src refspec main does not match any means your local branch is not called main, or it has no commits. Check with git branch and git log --oneline. Older Git versions create master by default. Rename it before pushing:
git branch -M main
git push -u origin mainAuthentication failures
GitHub no longer accepts your account password for Git operations over HTTPS. Use a personal access token as the password, or run gh auth login so the GitHub CLI stores credentials for you.
For SSH, test the connection on its own, which separates key problems from Git problems:
ssh -T [email protected]A greeting that includes your username means the key works. If you see Permission denied (publickey), the key is not loaded in your SSH agent or its public half is not added to your GitHub account. If the test works but a push still fails, run git remote -v. The remote may use an HTTPS URL, so the SSH key is never used.
A decision path
Exercise
You run git push -u origin main and get ! [rejected] main -> main (fetch first). The GitHub repository was created with a README. What two commands do you run next, and why not use --force?
Show answer
Run git pull origin main --allow-unrelated-histories and then git push -u origin main. If Git says the histories are unrelated, the flag lets it merge them. Forcing the push would overwrite the README commit on GitHub and lose that history.