Skip
Arish's avatar

34. How to Push Changes


Pushing Changes to Remote

The git push command uploads your local commits to a remote repository.

Basic Push

bash
1git push <remote> <branch>
2
3# Example
4git push origin main

First Push (Set Upstream)

When pushing a branch for the first time:

bash
1# Set upstream and push
2git push -u origin main
3
4# Or the long form
5git push --set-upstream origin main

After setting upstream, you can just use:

bash
1git push

Push Options

Push Current Branch

bash
1# Push current branch to its upstream
2git push
3
4# Push current branch to origin
5git push origin HEAD

Push All Branches

bash
1git push --all origin

Push Tags

bash
1# Push a specific tag
2git push origin v1.0.0
3
4# Push all tags
5git push origin --tags

Force Push

bash
1# Force push (overwrites remote - DANGEROUS!)
2git push --force origin main
3
4# Or short form
5git push -f origin main
6
7# Safer force push (fails if remote has new commits)
8git push --force-with-lease origin main

⚠️ Warning: Never force push to shared branches without team agreement!

Understanding Push

What Happens When You Push

Local Repository Remote Repository ┌──────────────┐ ┌──────────────┐ │ A ─ B ─ C ─ D│ git push │ A ─ B │ │ (main) │ ───────────▶ │ (main) │ └──────────────┘ └──────────────┘ After push: ┌──────────────┐ ┌──────────────┐ │ A ─ B ─ C ─ D│ │ A ─ B ─ C ─ D│ │ (main) │ │ (main) │ └──────────────┘ └──────────────┘

Push Rejected?

If someone else pushed first:

Local: A ─ B ─ C Remote: A ─ B ─ D

Git will reject your push:

bash
1git push origin main
2# error: failed to push some refs
3# hint: Updates were rejected because the remote contains work

Solution: Pull first, then push:

bash
1git pull origin main
2git push origin main

Push Workflow

Standard Workflow

bash
1# 1. Make changes
2echo "update" >> file.txt
3
4# 2. Stage
5git add file.txt
6
7# 3. Commit
8git commit -m "Update file"
9
10# 4. Push
11git push origin main

Feature Branch Workflow

bash
1# 1. Create feature branch
2git checkout -b feature/new-feature
3
4# 2. Work and commit
5git add .
6git commit -m "Add new feature"
7
8# 3. Push feature branch
9git push -u origin feature/new-feature
10
11# 4. Create Pull Request on GitHub
12
13# 5. After merge, delete branch
14git checkout main
15git pull origin main
16git branch -d feature/new-feature

Push Best Practices

1. Pull Before Push

bash
1# Always pull first to avoid conflicts
2git pull origin main
3git push origin main

2. Push Often

Don't let local changes accumulate:

bash
1# After each logical chunk of work
2git push

3. Review Before Pushing

bash
1# See what will be pushed
2git log origin/main..HEAD
3
4# See commits to push
5git diff origin/main..HEAD

4. Never Force Push to Shared Branches

bash
1# BAD - affects others
2git push -f origin main
3
4# OKAY - only for your own branches
5git push -f origin my-feature-branch

Summary

CommandDescription
git push origin mainPush main to origin
git push -u origin branchPush and set upstream
git pushPush current branch
git push --allPush all branches
git push --tagsPush all tags
git push -fForce push (dangerous!)

Practice pushing in the next exercise!