Fixing 'fatal: no upstream branch' and Push Rejected in Git

Isha GuptaIsha Gupta
4 min read

In this Article:

💡 What Was the Problem?

📖 Understanding the Errors

🛠️ How I Fixed It Step-by-Step

🧠 What I Learned

✅ Git Commands Going Forward

Let’s begin.

Recently, I ran into this error while pushing code from IntelliJ:

git push --set-upstream origin main
fatal: The current branch has no upstream branch. ! [rejected] main -> main (non-fast-forward) error: failed to push some refs to hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Terminologies:

originThe default name for GitHub remote repo
mainThe local branch name (same as remote in this case)
UpstreamThe remote branch your local branch is tracking
RebaseRewrites the commit history to apply your changes on top of updated remote commits
Fast-forwardA push or pull that doesn’t require merging, Git just “moves forward.”

So, my problem was

I had local changes, A local branch “main” and A Github Repo linked as the remote but my local main wasn’t linked to the remote main and was behind the remote main (meaning some changes were made on Github after my last pull)

What exactly happened:

After making some local commits, I tried to push them to GitHub, but I got the below error:

fatal: The current branch main has no upstream branch.

Then I tried this fix:

git push --set-upstream origin main

And I got a new error:

! [rejected] main -> main (non-fast-forward) 
error: failed to push some refs to 'https://github.com/Isha2103/repo.git' 
hint: Updates were rejected because the tip of your current branch is behind...

What Does This Mean?

  • No upstream branch: Git doesn’t yet know which remote branch my local branch is supposed to push to.

  • Non-fast-forward: The remote branch (main on GitHub) has changes that my local branch doesn't. If Git lets me push, I’d overwrite those changes.

Resolution Steps I took:

1. Staged and committed my changes locally

git add . 
git commit -m "Your commit message"

2. Tried to Push Without an Upstream

git push

Got error: The current branch has no upstream branch.

which means Git doesn’t yet know where on GitHub to push my main.

3. Tried to Set the Upstream and Push

git push --set-upstream origin main

Got error: rejected because your branch is behind its remote counterpart.

which means the GitHub version of ⁣main had changes that my local main did not. Git blocks this to prevent me from overwriting other changes on GitHub.

4. Rebased (Synced) My Local Main with Remote

git pull origin main --rebase

Git responded with:
* branch main -> FETCH_HEAD Successfully rebased and updated refs/heads/main.

This pulled the new changes from GitHub and applied my local commits on top. This is a clean way to integrate changes without a merge commit. Now my local main is fully up-to-date and has recent new changes.

Alternative way: if you prefer merging, just do

git pull origin main

5. Successfully Pushed The Changes

git push -u origin main

Git responded with:
Enumerating objects ...
main Branch 'main' set up to track remote branch 'main' from 'origin'.

This pushed my local main to GitHub and set the tracking link between my local main and remote (origin/main) so now I could use git pull or git push going forward to make any changes.

That means I don’t need to do --set-upstream now as we are now tracking remote main. And this was done using -u flag which sets the upstream link so future pushes/pulls can be done just with git push or git pull

Commands I would need from now on are just

git add . 
git commit -m "your message"
git push
git pull

After this push, your GitHub repo will be fully updated with your latest local changes.

Next time, to avoid this confusion:

  • Always pull before you push to make sure you're synced.

  • Use git status to see what's going on.

  • If you’re setting up a new repo, push right after the first commit to establish the upstream early.

So, errors like "no upstream branch" or "non-fast-forward" can be scary at first, but once you understand what Git is protecting you from, it becomes second nature.

In a nutshell, you need to do:

1. Stage and commit local changes

git add .
git commit -m "Your commit message"
  1. Sync With Remote Branch Using Rebase
git pull origin main --rebase 
or
git pull origin main
  1. Push and Set upstream
git push -u origin main

That’s all you need to do: Local changes → git add → git commit → git pull --rebase → git push -u

Hope this helped! Let me know if you’ve faced the same and this article has helped you.

Toodles!

0
Subscribe to my newsletter

Read articles from Isha Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Isha Gupta
Isha Gupta

Wanna learn together? Let's catch up on Twitter (X)!