Common causes
- You ran git init locally, committed, then added a GitHub/GitLab remote that was created with a README, license or .gitignore
- The project was uploaded to the server, git init was run there, and it is now pulling from a separate repository
- The remote URL points to a different repository than you think
- The remote history was rewritten from scratch (e.g. an orphan branch or a re-created repository)
- You are merging two separate projects intentionally, such as combining repositories
How to fix it
- Confirm the remote is correct. Run git remote -v and git log --oneline -3 origin/main after git fetch. If the remote is the wrong repository, fix it with git remote set-url origin <url> instead of merging.
- Allow the merge once. If both histories really belong together, run git pull origin main --allow-unrelated-histories. Git will join them with a merge commit.
- Resolve conflicts. Files that exist on both sides (README.md, .gitignore, LICENSE) usually conflict. Edit them, git add the results and git commit to finish the merge.
- Push the combined history. Run git push -u origin main. From now on pulls work normally because the histories share a commit.
- Alternative: start from the remote. If your local repo has little history, clone the remote into a new folder, copy your working files in, commit and push. This avoids the merge commit entirely.
Shell
git remote -v
git fetch origin
git pull origin main --allow-unrelated-histories
# fix conflicts in README.md / .gitignore, then
git add README.md .gitignore
git commit
git push -u origin main How to stop it happening again
- Create the remote repository empty (no README or license) when pushing an existing project
- Or clone the remote first and add your code to that clone
- On servers, deploy with git clone rather than git init in an uploaded folder