Common Git Errors: Push and Pull

Updated · 13 min read
mediumGit 2.53giterrorstroubleshootingpushpullinterview

A Git error message is Git's explanation of why it refused to do something, usually followed by hint: lines that suggest a fix. Most push and pull errors come from one situation: your copy of a branch and the server's copy have moved apart. This page shows each common error exactly as Git prints it, from real runs, then what it means, how to fix it, and how to avoid it. Nothing here needs you to delete the repository and clone again.

Reading an error message is like reading a parking ticket: it already says what you did and where. Read it to the end before searching the internet.

How to Read Any Error

  1. Find the first fatal: or error: line. That is the actual problem. Lines starting with hint: are suggestions.
  2. Run git status. It says which branch you are on, whether a merge or rebase is in progress, and how your branch compares with its upstream.
  3. Look at the graph: git log --oneline --graph --all -10 shows how your branch and origin/... relate.
  4. Fix the cause, not the symptom. --force makes most push errors disappear, and destroys other people's work.
The parts of a Git error message

The Starting Point

As in "Git Remote", the bare repository fee-tracker.git stands in for the server, and navika and advitha are each person's computer:

Terminal
git init --quiet --bare fee-tracker.git
git clone --quiet fee-tracker.git navika 2>/dev/null
cd navika
echo 'Term 1 fee: $12,000' > fees.txt
git add fees.txt
git commit --quiet -m "Add the term fee"
git push --quiet -u origin main
cd ..
git clone --quiet fee-tracker.git advitha
git -C advitha config user.name "Advitha Anamaneni"
git -C advitha config user.email advitha@example.com
Output
 

Updates Were Rejected (Non-Fast-Forward)

Advitha pushes a commit. Navika, who hasn't fetched it, pushes her own:

Terminal
cd advitha
echo 'Late fee: $250' >> fees.txt
git commit --quiet -am "Add the late fee"
git push --quiet
cd ../navika
echo 'Exam fee: $800' > exam.txt
git add exam.txt
git commit --quiet -m "Add the exam fee"
git push
Output
To /home/navika/projects/fee-tracker.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to '/home/navika/projects/fee-tracker.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

What it means: the server's main has a commit that Navika doesn't have. Accepting her push would replace the server's main with her version and lose Advitha's commit, so Git refuses. Git says (fetch first) when your clone hasn't even seen the new commit, and (non-fast-forward) when it has (after a fetch); both mean your branch doesn't simply extend the server's.

The fix: get the other work first, then push. Either merge or rebase it in (the next error shows that choice):

Terminal
git pull --quiet --rebase
git push --quiet
git log --oneline --graph
Output
* 028deb6 (HEAD -> main, origin/main, origin/HEAD) Add the exam fee
* f9641ba Add the late fee
* 6b651a0 Add the term fee

Avoid it: git pull (or git fetch and look) before you start work and before pushing. Don't use git push --force here: it would delete Advitha's commit from the server. If you must force-push your own rewritten branch, use --force-with-lease (see "Force Push Safely").

You Have Divergent Branches

Both of them commit again, and Advitha runs a plain git pull:

Terminal
echo 'Hostel fee: $9,000' >> fees.txt
git commit --quiet -am "Add the hostel fee"
git push --quiet
cd ../advitha
echo 'Bus fee: $1,500' > bus.txt
git add bus.txt
git commit --quiet -m "Add the bus fee"
git pull
Output
From /home/navika/projects/fee-tracker
   f9641ba..9044480  main       -> origin/main
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
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.

What it means: her branch and origin/main both have new commits (they have diverged), and Git doesn't know whether she wants to merge them or rebase hers on top. Since Git 2.33.1, it stops and asks you to choose instead of silently merging. The fetch already happened, so nothing is lost.

The fix: choose, for this pull or for good:

Terminal
git config pull.rebase true
git pull --quiet
git log --oneline --graph -3
Output
* ebc8ad7 (HEAD -> main) Add the bus fee
* 9044480 (origin/main, origin/HEAD) Add the hostel fee
* 028deb6 Add the exam fee

Explanation: pull.rebase true makes every pull rebase local commits on top of the server's; false merges instead, and pull.ff only refuses unless a fast-forward is possible. Use --global to set it everywhere. See "Git Pull" for when to choose which.

The Current Branch Has No Upstream Branch

Advitha starts a branch and pushes it:

Terminal
git push --quiet
git switch --quiet -c feature/books
echo 'Books fee: $600' > books.txt
git add books.txt
git commit --quiet -m "Add the books fee"
git push
Output
fatal: The current branch feature/books has no upstream branch.
To push the current branch and set the remote as upstream, use
 
    git push --set-upstream origin feature/books
 
To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.

What it means: the new branch has no upstream (no branch on the server it is connected to), so git push doesn't know where to send it.

The fix: run the command Git suggests, once:

Terminal
git push -u origin feature/books
Output
To /home/navika/projects/fee-tracker.git
 * [new branch]      feature/books -> feature/books
branch 'feature/books' set up to track 'origin/feature/books'.

Avoid it: git config --global push.autoSetupRemote true makes the first git push of a new branch set up the upstream automatically, as the message mentions.

Src Refspec Does Not Match Any

Srihitha creates a new repository and tries to push before her first commit:

Terminal
cd ..
git init --quiet srihitha
cd srihitha
git remote add origin ../fee-tracker.git
git push -u origin main
Output
error: src refspec main does not match any
error: failed to push some refs to '../fee-tracker.git'

What it means: "src refspec main" is the local branch main, and it doesn't exist yet, because a branch is created by its first commit. The same error appears when the branch has a different name, such as master, than the one you typed.

The fix: commit first, or push the branch that does exist (git branch shows its name). Here, she clones instead, because the project already exists:

Terminal
cd ..
rm -rf srihitha
git clone --quiet fee-tracker.git srihitha
git -C srihitha log --oneline -1
Output
ebc8ad7 (HEAD -> main, origin/main, origin/HEAD) Add the bus fee

Refusing to Merge Unrelated Histories

Srihitha's colleague started a separate repository with a README, and tries to pull the Fee Tracker into it:

Terminal
git init --quiet readme-project
cd readme-project
echo "# Fee Tracker" > README.md
git add README.md
git commit --quiet -m "Add a README"
git pull --no-rebase ../fee-tracker.git main
Output
From ../fee-tracker
 * branch            main       -> FETCH_HEAD
fatal: refusing to merge unrelated histories

What it means: the two histories have no commit in common: they were started separately. Git refuses, because this is usually a mistake, such as pulling from the wrong repository, or creating a repository on GitHub with a README and then pushing an existing local project to it.

The fix: if you really want to join them, say so:

Terminal
git pull --quiet --no-rebase --no-edit --allow-unrelated-histories ../fee-tracker.git main
ls
Output
README.md
bus.txt
exam.txt
fees.txt

Avoid it: when putting an existing project on a hosting platform, create the remote repository empty (no README, .gitignore or license), then push.

Push Rejected Because of Size

Servers limit how much can be pushed. GitHub rejects any single file over 100 MB, and other platforms have their own limits. Here, the stand-in server allows at most 1 MB per push:

Terminal
cd ..
git -C fee-tracker.git config receive.maxInputSize 1m
cd navika
git pull --quiet
head -c 3000000 /dev/urandom > scan.bin
git add scan.bin
git commit --quiet -m "Add the receipt scan"
git push 2>&1 | grep -E "error|fatal|rejected"
Output
remote: fatal: pack exceeds maximum allowed size
error: remote unpack failed: unpack-objects abnormal exit
 ! [remote rejected] main -> main (unpacker error)
error: failed to push some refs to '/home/navika/projects/fee-tracker.git'

What it means: the server refused the pack because it is too big. The file is in a commit, so even after deleting it, a new commit isn't enough: the old commit still contains it.

The fix: if the commit isn't pushed yet, take the file out of it, then push:

Terminal
git rm --cached --quiet scan.bin
echo "scan.bin" >> .gitignore
git add .gitignore
git commit --quiet --amend -m "Ignore the receipt scan"
git push --quiet
git log --oneline -1 --stat
Output
48e2448 (HEAD -> main, origin/main, origin/HEAD) Ignore the receipt scan
 .gitignore | 1 +
 1 file changed, 1 insertion(+)

Explanation: git commit --amend replaced the commit, so the large file is in no commit that is pushed. If the file is several commits back, use an interactive rebase (see "Interactive Rebase") or git filter-repo (see "Removing Files from History"). For files that must be versioned, use Git LFS (see "Git Large File Storage (LFS)").

Common Mistakes

Force-pushing to get past "rejected". It deletes other people's commits on the server. Pull first.

Deleting the folder and cloning again. It loses unpushed commits and stashes. Every error here is fixable in place.

Ignoring the hints. Git's hint: lines usually contain the exact command you need.

Creating the GitHub repository with a README for an existing project. It causes unrelated histories. Create it empty.

Under the Hood

On git push, Git first asks the server for its branch tips. For each branch, it checks whether the server's commit is an ancestor of yours (git merge-base --is-ancestor). If not, the update isn't a fast-forward, and Git rejects it locally, before sending anything, with [rejected] ... (fetch first) when your copy hasn't even seen the server's commit, or (non-fast-forward) when it has. The server also checks: receive.denyNonFastForwards, size limits such as receive.maxInputSize, and pre-receive hooks (see "Git Hooks") can all refuse the update, and their messages appear prefixed with remote:. On git pull, the fetch always completes first, so any error during the merge or rebase part leaves the downloaded commits safely in origin/<branch>.

Interview Corner

Your push was rejected with "non-fast-forward". What do you do?

Someone pushed first. Run git pull --rebase (or git pull with a merge), fix any conflicts, run the tests and push again. Don't force-push a shared branch.

What does "You have divergent branches and need to specify how to reconcile them" mean?

Both your branch and the remote have new commits. Git asks whether to merge or rebase: run git pull --rebase or --no-rebase once, or set pull.rebase in your config.

What does "fatal: The current branch has no upstream branch" mean?

The branch isn't connected to a remote branch yet. Push with git push -u origin <branch>, or set push.autoSetupRemote true.

What does "refusing to merge unrelated histories" mean?

The two branches have no common commit. If joining them is intended, add --allow-unrelated-histories; otherwise check that you are pulling from the right repository.

Practice

Challenge: rejected again. Advitha pushes a new sports fee. Without pulling, Navika commits a change to fees.txt. Fix the rejected push and print the last three commits.

What does Git print? Before you open the solution, decide which commit ends up on top.

Show solution
Terminal
cd ../advitha
git switch --quiet main
git pull --quiet
echo 'Sports fee: $400' > sports.txt
git add sports.txt
git commit --quiet -m "Add the sports fee"
git push --quiet
cd ../navika
echo "Note: fees are in US dollars" >> fees.txt
git commit --quiet -am "Explain the currency"
git push --quiet 2>/dev/null || echo "Push rejected, pulling first"
git pull --quiet --rebase
git push --quiet
git log --oneline -3
Output
Push rejected, pulling first
bf0cd09 (HEAD -> main, origin/main, origin/HEAD) Explain the currency
5e7df3b Add the sports fee
48e2448 Ignore the receipt scan

The first push was rejected, because Advitha's sports fee commit was on the server. After git pull --rebase, Navika's commit sits on top of it, and the push succeeds.

Checkpoint. Make two clones of one repository, and reproduce and fix three errors from this page: a rejected push, divergent branches and a missing upstream.

Quick check: push and pull errors

4 questions
1What usually causes "Updates were rejected because the remote contains work that you do not have locally"?
2Which setting stops the "divergent branches" message by always rebasing?
3What does git push -u origin feature/books do that git push couldn't?
4"error: src refspec main does not match any" means:

Key Takeaways

Quick reference

ErrorFix
[rejected] ... (fetch first) / (non-fast-forward)git pull --rebase, then git push
You have divergent branchesgit pull --rebase or --no-rebase, or set pull.rebase
has no upstream branchgit push -u origin <branch>
src refspec <branch> does not match anyCommit first, or check git branch
refusing to merge unrelated histories--allow-unrelated-histories (if intended)
Push too largeRemove the file from unpushed commits; use Git LFS

Was this tutorial helpful?

Lesson 102 of 106