Git without fear · Chapter 22 of 28

Force, but with a lease

By the end of this chapter you can push a branch you have rewritten without erasing a teammate's commit on GitHub. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Teamwork
  • 4 tasks
  • Git
  • In the all labs plan

The first 2 chapters of Git without fear are free. This one comes with the all labs plan.

Why you need this

Yesterday you rebased your menu-photos branch onto the new main (cutting chai is 30 now), the way Kabir showed you. This morning git push says no. Bunty: "Easy, git push --force. I do it all the time." What neither of you knows yet: last night Ravi pushed "Credit the photographer" to menu-photos on GitHub. You fetched only main, so your laptop has never seen it.

A rebase makes new commits, so a rewritten branch can never be pushed the normal way again. --force gets it up there and silently deletes Ravi's commit. A pull does not help either: it merges the old copy of your commit back in, and "List the menu photos" ends up in the branch twice.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Keep Ravi's commit on GitHub's menu-photos

    Ravi added a commit to menu-photos on GitHub. Your laptop has not seen it yet.

    1. Run git fetch in the terminal. It downloads what GitHub has and does not change your branch.

    2. Show the history of every branch in a graph. Find the commit "Credit the photographer".

    3. Rebase your menu-photos onto GitHub's copy of menu-photos. Now your branch has Ravi's commit too.

    To pass: when you are done, menu-photos on GitHub still has the commit "Credit the photographer".

  2. Task 2

    Put menu-photos on top of the latest main, on GitHub

    Your menu-photos must sit on top of the newest main, with each commit only once.

    1. After you have Ravi's commit, rebase menu-photos onto GitHub's main again.

    2. Do not use git pull here. A pull makes a merge and brings your old commit back a second time.

    3. Show the graph. It is one straight line: the menu, "Raise cutting chai to 30", your photos commit, Ravi's credit.

    4. Push the branch to GitHub (task 4 shows the safe way).

    To pass: on GitHub, menu-photos has all of GitHub's main in its history, and "List the menu photos" is in it exactly once.

  3. Task 3

    Never force-push without a lease

    A plain force push would delete Ravi's work on GitHub. Never use it in this chapter.

    1. Do not type git push with --force, with -f, or with a + before the branch name.

    2. Push your rewritten menu-photos with --force-with-lease instead (task 4).

    3. If you already typed a plain force push, run lab reset and start again.

    To pass: menu-photos is pushed to GitHub, and no push you typed in the chapter folder used --force, -f or + without --force-with-lease. A push that failed also counts.

  4. Task 4 · interview question

    Interview: push the rewritten branch with --force-with-lease

    Interview question: how do you push a rewritten branch safely?

    1. First do tasks 1 and 2: fetch, then the two rebases.

    2. Run git push --force-with-lease. Git prints forced update.

    3. If Git refuses the push, you have not fetched yet. Fetch, rebase, and push again.

    To pass: you ran git push --force-with-lease in the chapter folder, and it worked. A push that Git refused does not count, so run it again.

What you will learn

  • Why a rewritten branch is rejected: GitHub's branch has commits yours no longer contains.
  • --force-with-lease: overwrite GitHub's branch only if it is still where you last saw it.
  • Fetch, look, combine: bring a teammate's commit into your branch before you overwrite anything.

More in Teamwork

Build "Force, but with a lease" yourself

Your first week on Tapri’s team, and every Git situation it throws at you. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Git without fear are free. This one comes with the all labs plan.