Lesson Completion
Back to course

git push

Beginner
9 minutes4.8GitPlay to LearnLearn by Story

The Hook (The "Byte-Sized" Intro)

Until you push, your brilliant code exists on exactly one machine in the universe — yours. git push sends your commits from your local repository to the remote, making them visible to your team, triggering CI pipelines, and backing up your work. A commit that isn't pushed is a commit that doesn't count.

📖 What is git push?

git push uploads local commits to a remote repository. It sends the commits from your current branch (or a specified branch) to the corresponding remote branch, updating the remote's history.

Conceptual Clarity

  • git push sends commits — it does NOT send uncommitted changes
  • You can only push if the remote branch is not ahead of yours (otherwise, pull first)
  • The first push of a new branch requires -u to set up tracking
  • After tracking is set, plain git push works without arguments
  • Push is branch-specific — it only sends the branch you specify (or the current one)

What happens during push:

  1. Git compares your local branch with the remote branch
  2. If the remote is behind, Git fast-forwards it
  3. If the remote has diverged, Git rejects the push (pull first!)

Real-Life Analogy

git push is like uploading your finished document to the team's shared drive. Until you upload, only you can see it. After uploading, everyone can access it, comment on it, and build on it.

Visual Architecture

flowchart LR LOCAL["💻 Local Repo"] -->|"git push"| REMOTE["☁️ Remote Repo"] REMOTE --> CI["⚙️ CI/CD Pipeline"] REMOTE --> TEAM["👥 Team Access"] REMOTE --> BACKUP["💾 Backup"] style LOCAL fill:#1a1a2e,stroke:#e94560,color:#e94560 style REMOTE fill:#0f3460,stroke:#53d8fb,color:#53d8fb style CI fill:#1a1a2e,stroke:#ffd700,color:#ffd700 style TEAM fill:#1a1a2e,stroke:#ffd700,color:#ffd700 style BACKUP fill:#1a1a2e,stroke:#ffd700,color:#ffd700

Why It Matters

  • Collaboration: Your team can't see your work until you push.
  • CI/CD: Pushes trigger automated tests, builds, and deployments.
  • Backup: Pushed commits are safe even if your laptop dies.
  • Code review: Pushes to feature branches enable pull requests and reviews.

Code

bash
# ─── Push current branch (tracking already set) ─── git push # Sends local commits to the tracked remote branch # ─── First push of a new branch (set upstream) ─── git push -u origin feature/login # -u sets origin/feature/login as the tracking branch # After this, just 'git push' works # ─── Push to a specific remote and branch ─── git push origin main # ─── Push all branches at once ─── git push --all origin # ─── Push tags ─── git push origin v1.0.0 # Push one tag git push origin --tags # Push all tags # ─── Delete a remote branch via push ─── git push origin --delete feature/old # ─── Force push (use with extreme caution) ─── git push --force-with-lease origin feature/login # Safer than --force: fails if remote has new commits # Use only for amended/rebased commits on YOUR branch # ─── Dry run (see what would be pushed) ─── git push --dry-run

Push Cheat Sheet

CommandPurpose
git pushPush current branch (tracking set)
git push -u origin <branch>First push + set upstream tracking
git push origin --tagsPush all tags
git push origin --delete <branch>Delete a remote branch
git push --force-with-leaseForce push safely (after amend/rebase)
git push --dry-runPreview what would be pushed

Key Takeaways

  • git push sends local commits to the remote — uncommitted changes are NOT sent.
  • Use -u on the first push to set up tracking; then plain git push works.
  • Never use --force on shared branches. Use --force-with-lease if you must force-push.
  • Push triggers CI/CD, enables code review, and backs up your work.

Interview Prep

  • Q: What does git push -u origin main do? A: It pushes the local main branch to the remote origin and sets the upstream tracking relationship. After this, git push and git pull on this branch will default to origin/main without needing to specify the remote and branch name.

  • Q: What happens if you try to push when the remote has new commits you don't have? A: Git rejects the push with a "non-fast-forward" error. You must first pull (or fetch + merge/rebase) the remote changes, resolve any conflicts, and then push again.

  • Q: What is the difference between --force and --force-with-lease? A: --force overwrites the remote branch unconditionally, potentially losing commits pushed by others. --force-with-lease only overwrites if the remote branch is at the commit you expect — if someone else pushed new commits, it fails safely.

Topics Covered

Git RemotesGit Fundamentals

Tags

#git#push#remote#beginner-friendly

Last Updated

2026-02-12