Pushing and Pulling Code: git push -u origin main and git pull0%

Pushing and Pulling Code: git push -u origin main and git pull

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

Pushing and Pulling Code: git push -u origin main and git pull

Once your local repository is connected to a remote on GitHub, you transfer commits back and forth using git push (uploading local changes to the cloud) and git pull (downloading cloud updates into your local machine).


1. Pushing Code: git push

The git push command exports commits from your local repository to a branch on the remote server.

The First Push: Setting Upstream Tracking (-u)

The very first time you push a newly created branch to a remote, you must tell Git which remote branch should track your local branch:

Bash / Terminal
git push -u origin main

What does the -u (or --set-upstream) flag do?

  • It binds your local main branch to the remote branch origin/main.
  • For all future pushes and pulls on this branch, you no longer need to type origin main. You can simply type:
Bash / Terminal
git push
git pull

Terminal Output:

Output
Enumerating objects: 15, done.
Counting objects: 100% (15/15), done.
Delta compression using up to 8 threads
Compressing objects: 100% (12/12), done.
Writing objects: 100% (15/15), 4.20 KiB | 2.10 MiB/s, done.
Total 15 (delta 2), reused 0 (delta 0)
To github.com:sumit-msk/ecommerce-dashboard.git
* [new branch] main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.

2. Pulling Code: git pull vs. git fetch

When working in a team or across multiple computers, teammates will push new commits to GitHub. You must pull these commits down to stay updated.

Deep Dive: What is git pull Really Doing?

git pull is actually a combination of two underlying Git commands:

Output
git pull
|
+---------------+---------------+
| |
v v
git fetch git merge
(Download new commits (Merge remote commits
into origin/main) into local branch)
  1. 1
    git fetch: Downloads all commits, branches, and tags from the remote repository into your local .git folder, updating remote-tracking branches (like origin/main), but leaves your working directory untouched.
  2. 2
    git merge origin/main: Integrates the fetched commits into your active local branch.
Bash / Terminal
# Recommended safe team workflow:
# 1. Fetch remote updates and review
git fetch origin
 
# 2. Inspect what changed before merging
git log HEAD..origin/main --oneline
 
# 3. Merge cleanly
git merge origin/main

Or execute both in one command:

Bash / Terminal
git pull origin main

3. Dealing with Rejected Pushes (Non-Fast-Forward)

If a colleague pushed commits to GitHub while you were working locally, GitHub will reject your push with:

Output
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'github.com:user/repo.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.

The Standard Fix:

  1. 1
    Pull the colleague's commits down to your local machine:
Bash / Terminal
git pull origin main
  1. 1
    If there are merge conflicts, resolve them and commit.
  2. 2
    Now push your combined work cleanly:
Bash / Terminal
git push origin main
Important Warning
[!CAUTION] Never use git push --force on shared production branches! Force-pushing overwrites and deletes your teammates' commits from the remote server.

Interactive Quiz

Practice Quiz

Question 1
What does the command git push -u origin main accomplish?
Question 2
What two operations are combined when you run git pull?
Question 3
How does git fetch differ from git pull?
Question 4
Why does GitHub reject a push with a (fetch first) non-fast-forward error?
Question 5
Why is running git push --force strongly discouraged on shared collaborative branches?
Next Lesson

Forking Repositories and Cloning to Local Machine (git clone)

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext