MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Pushing and Pulling Code: git push -u origin main and git pull0%
Study Materials
Git & GitHub BasicsProgress 0%
1.1What is Version Control? (Local, Centralized vs Distributed VCS)1.2Why Git? History, Architecture & Core Benefits1.3The Three Trees: Working Directory, Staging Area & Git Repository1.4Installing Git and Initial Configuration (git config user.name / email)
5.1What is GitHub? Remote Hosting, Social Coding & Open Source5.2Setting up SSH Keys, Personal Access Tokens (PAT) and Authentication5.3Creating a Remote Repository and Connecting with git remote add5.4Pushing and Pulling Code: git push -u origin main and git pull
HomeTutorialsGit & GitHub BasicsPushing and Pulling Code: git push -u origin main and git pull
Forking Repositories and Cloning to Local Machine (git clone)

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

Beginner12 min read•Updated: 2026-09-15•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

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:

1git 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:
1 git push
2 git pull

Terminal Output:

1Enumerating objects: 15, done.
2Counting objects: 100% (15/15), done.
3Delta compression using up to 8 threads
4Compressing objects: 100% (12/12), done.
5Writing objects: 100% (15/15), 4.20 KiB | 2.10 MiB/s, done.
6Total 15 (delta 2), reused 0 (delta 0)
7To github.com:sumit-msk/ecommerce-dashboard.git
8 * [new branch] main -> main
9Branch '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:

1 git pull
2 |
3 +---------------+---------------+
4 | |
5 v v
6 git fetch git merge
7(Download new commits (Merge remote commits
8 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.
1# Recommended safe team workflow:
2# 1. Fetch remote updates and review
3git fetch origin
4 
5# 2. Inspect what changed before merging
6git log HEAD..origin/main --oneline
7 
8# 3. Merge cleanly
9git merge origin/main

Or execute both in one command:

1git 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:

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

The Standard Fix:

  1. 1
    Pull the colleague's commits down to your local machine:
1 git pull origin main
  1. 1
    If there are merge conflicts, resolve them and commit.
  2. 2
    Now push your combined work cleanly:
1 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• Knowledge Check

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.

Related Lessons

Previous LessonNext Lesson
Creating a Remote Repository and Connecting with git remote addForking Repositories and Cloning to Local Machine (git clone)

Practice Quiz

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

Mentor-Led Practical Training

Master Git & GitHub Basics with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext