World map

 

BM: Baca dalam Bahasa Melayu

World 4: Hello GitHub

Force push, safely

You will learn: when a force push is OK, and how --force-with-lease protects your team.

The problem

You pushed “Add about pgae”. Oops! You fixed the message with --amend. Now git push is rejected. git push --force is tempting. What’s the risk?

Pick one answer.

Watch

git push --force replaces GitHub's main with yours. Aisyah's commit and the old typo commit vanish from GitHub.

Your laptop~/diary
  1. e82b02fAdd about page (only on your laptop)main (HEAD is here)
  2. f91c599Add about pgae (fetched, not in main yet)origin/main
  3. ea2c020Add purple style
  4. f6c78e5Add home page
  5. 2efd0f2Add README
1 ahead · 1 behind origin/main
GitHubgithub.com/you/diary
  1. f0f8b99Add Aisyah's day 2 · Aisyah (only on GitHub)main
  2. f91c599Add about pgae
  3. ea2c020Add purple style
  4. f6c78e5Add home page
  5. 2efd0f2Add README

A force push: Make the remote branch match yours, even if that throws away commits there. says: make GitHub’s main exactly like mine. Aisyah pushed after your last fetch. Her commit just vanished.

Try it

Build the safer force push.

Tap the pieces in the right order.

…

Watch

git push --force-with-lease checks GitHub first. GitHub moved since your last fetch, so the push stops and the parcel comes back. Aisyah's commit stays.

Your laptop~/diary
  1. e82b02fAdd about page (only on your laptop)main (HEAD is here)
  2. f91c599Add about pgae (fetched, not in main yet)origin/main
  3. ea2c020Add purple style
  4. f6c78e5Add home page
  5. 2efd0f2Add README
1 ahead · 1 behind origin/main
GitHubgithub.com/you/diary
  1. f0f8b99Add Aisyah's day 2 · Aisyah (only on GitHub)main
  2. f91c599Add about pgae
  3. ea2c020Add purple style
  4. f6c78e5Add home page
  5. 2efd0f2Add README

--force-with-lease checks first. Is GitHub still where my last fetch saw it? Aisyah pushed since, so it stops. Her work stays safe.

Check

Force push: OK, or needs work? Swipe each one.

Swipe right for good, left for needs work. Or use the buttons.

1 / 4

Force push your own branch after you rebase it

Force push your own branch after you rebase it

Try it

This time nobody pushed since your last fetch. Send your fixed commit safely.

Your laptop~/diary
  1. a26f6d8Add about page (only on your laptop)main (HEAD is here)
  2. f91c599Add about pgae (fetched, not in main yet)origin/main
  3. ea2c020Add purple style
  4. f6c78e5Add home page
  5. 2efd0f2Add README
1 ahead · 1 behind origin/main
GitHubgithub.com/you/diary
  1. f91c599Add about pgaemain
  2. ea2c020Add purple style
  3. f6c78e5Add home page
  4. 2efd0f2Add README

Tap an action. Watch the courier.

You did it!

You can now force push only when it’s safe, with –force-with-lease.

Go deeper (optional)

The lease trusts your origin/main. Some editors fetch quietly in the background, which updates it. Then the lease may pass without you seeing the new commits. --force-if-includes adds one more check for that. Shared mistakes on main? Use git revert instead.