Syncing Remotes (git push, git pull)
π’ Beginner
π Definition
git push uploads local repository commits to a remote server (like GitHub). git pull fetches latest commits from a remote server and automatically merges them into your active local working branch (git pull = git fetch + git merge).
π Multilingual Explanation
English
git push uploads local commits to the remote server. The -u flag (--set-upstream) links your local branch to the remote branch so future pushes require typing only git push. git pull downloads remote changes and merges them into your active local branch.
Hindi
Local computer se commits ko GitHub par upload karne ke liye git push ka upyog hota hai. Pehli baar push karte waqt -u (--set-upstream) flag lagayein (git push -u origin main). GitHub se naye commits ko local computer par download karke merge karne ke liye git pull run karein.
Marathi
Local commits GitHub var upload karanyasathi git push vaparatat. Pehlya veli git push -u origin main chalvave. GitHub varil navin badal local computer var aannyasathi git pull command vaparali jaate.
Hinglish
git push aapke local commits ko GitHub par live kar deta hai. First time push par git push -u origin main use karein. Remote repository par teammates ke dwaara kiye gaye new commits ko apne local code mein merge karne ke liye git pull Type karein.
π€ Why Do We Use They?
Without pushing and pulling, code stays trapped on a single developerβs laptop. Pushing publishes your completed work to team members; pulling keeps your local workspace synchronized with team contributions.
π§ Simple Explanation
Think of pushing and pulling like cloud storage synchronization:
git push(Upload): Uploading your local photos to Google Photos or iCloud so others can view them.git pull(Download & Sync): Downloading the latest photos shared by family members into your local gallery.
π Syntax & Command Patterns
1. Pushing Local Commits (git push)
# First time push: Uploads branch AND sets upstream tracking link
git push -u origin main
# Subsequent pushes on a tracked branch:
git push
# Push a specific feature branch to remote
git push -u origin feature-login
# Delete a remote branch on GitHub
git push origin --delete feature-login
2. Pulling Remote Changes (git pull)
# Pull and merge latest changes from tracked upstream branch
git pull
# Explicitly pull from origin main
git pull origin main
# Pull with rebase instead of merge commit
git pull --rebase origin main
π‘ Practical Example
Full terminal syncing workflow:
# 1. Make local changes and commit
echo "console.log('v1.1');" >> app.js
git commit -am "Update app version to v1.1"
# 2. Push to GitHub origin main
git push -u origin main
# 3. Pull latest changes made by team members on GitHub
git pull
π Command Breakdown
git push: Invokes upload engine.-u(--set-upstream): Remembers remoteoriginand branchmainso future pushes require onlygit push.git pull: Executesgit fetch(downloads remote commits) followed immediately bygit merge(combines remote commits into local branch).
π Terminal Output
$ git push -u origin main
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (5/5), 450 bytes | 450.00 KiB/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To https://github.com/Rahul/my-app.git
* [new branch] main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.
β οΈ Common Mistakes & Push Rejection
- Push Rejection (
Updates were rejected because the remote contains work...): Attemptinggit pushwhen GitHub contains newer commits pushed by teammates that you donβt have locally yet!- Fix: Run
git pull origin mainfirst, resolve any merge conflicts if necessary, and then rungit push.
- Fix: Run
- DANGER β Force Pushing (
git push --force): Overwriting remote commits with your local history. Never force push to sharedmainbranches! (Use--force-with-leaseif force pushing on personal feature branches).
π‘οΈ Safety / Important Notes
- Always run
git statusto ensure your working tree is clean before runninggit pull.
π Real-World Usage
Developers execute git pull every morning when starting work to get teammatesβ latest code, and execute git push before ending their workday.
π§ͺ Try It Yourself
- Verify your active branch using
git branch. - Practice the command syntax for pushing a branch named
feature-auth:git push -u origin feature-auth.
π― Mini Challenge
Explain in your own words why Git rejects git push if your teammate pushed new commits to GitHub while you were working offline.
π Related Topics
π§ Navigation
| β Home | β Previous: Remotes | Next: Fetching β |