Git disasters · Chapter 2 of 14

A deleted branch with unmerged work

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Lost and found
  • 3 tasks
  • Git
  • Free chapter

Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.

The lesson

By the end of this case

You can bring back a branch deleted with -D from the reflog, say how long Git keeps it, and make sure new branches reach GitHub on their own.

The ticket

TAP-915: The Diwali menu branch is gone. Assigned to you by Kabir. Priority: high. Since: this morning's clean-up.

Bunty, in the dev channel at 11:20 am: "was cleaning up old branches. git said festive-menu was not fully merged but i was SURE it was, so i used the capital D. it was not merged. kesar chai, the diwali week plan, the combo, three commits, none pushed. meera's printing the menu cards tomorrow"

His terminal now:

$ git branch
* main
$ git log --oneline
0a057a5 (HEAD -> main, origin/main) Add the samosa photo
684b4b0 Add the menu and prices

Kabir: "He switched branches a couple more times after the delete, so do not just take the top line of anything. Find the branch's last commit, give it its name back, get it onto GitHub this time, and make sure the next branch he makes does not live only on his laptop."

What deleting a branch deletes

A branch is a ref: a small file holding one commit's hash. Each commit points at its parent, so that one name keeps the whole line of work reachable. git branch -d deletes the name only if the work is merged somewhere; -D deletes it anyway:

$ git branch -d trip
error: The branch 'trip' is not fully merged.
If you are sure you want to delete it, run 'git branch -D trip'.
$ git branch -D trip
Deleted branch trip (was 97b64a7).

The commits are not deleted. They are unreachable: no branch or tag leads to them, so git log and git branch stop showing them. They stay in the object store until git gc prunes them.

HEAD's diary

git reflog is the history of where HEAD has been: every commit made, every switch, every reset, newest first. On a tiny repository where a trip branch was deleted:

$ git reflog
5624724 HEAD@{0}: checkout: moving from trip to main
97b64a7 HEAD@{1}: commit: Plan the Ooty trip
5624724 HEAD@{2}: checkout: moving from main to trip
5624724 HEAD@{3}: commit (initial): Start a todo list

Read it bottom to top as a story. The entry just before "moving from trip to main" is the last commit made on trip. HEAD@{1} names it, and so does its hash, and a branch is just a name:

$ git branch trip HEAD@{1}
$ git log --oneline -1 trip
97b64a7 Plan the Ooty trip

The numbers shift every time HEAD moves, so check which entry you mean with git log before you name it. The reflog is local: only the laptop where the commits were made has it, and entries for unreachable commits expire after 30 days by default.

Getting work off the laptop

The work was only at risk because it existed in one place. A plain push of a new branch stops and asks for an upstream:

$ git push
fatal: The current branch notes has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin notes

To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.

That last sentence is the guard.

Your turn

Bunty's repository is open in the terminal (lab reset rebuilds it). Write the hash of festive-menu's last commit into answer.txt. Bring the branch back at exactly that commit and push it, so GitHub has it too. Then set Git up so a plain git push of a new branch sends it to GitHub; the check tries that in a copy of your repository. Then answer Kabir's interview question below.

What you build: 3 tasks

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

  1. Task 1

    See what happened: find festive-menu's last commit

    Bunty deleted the branch festive-menu with a forced delete. Its three commits were never merged or pushed.

    1. Run git reflog. It lists every commit HEAD was on, newest first.

    2. Find the last commit Bunty made on festive-menu, before he moved to main. Its message is "Add the Diwali combo".

    3. Write its hash into answer.txt in the case's folder.

    To pass: answer.txt has the hash of "Add the Diwali combo", and no other hash. A short hash of 7 characters or more is fine.

  2. Task 2

    Recover: festive-menu back, here and on GitHub

    Bring the branch back, and put a copy on GitHub so it is not only on this laptop.

    1. Make a branch called festive-menu at the commit you found.

    2. Push festive-menu to GitHub, and set its upstream.

    To pass: festive-menu points exactly at "Add the Diwali combo", here and on GitHub. One of the two commits before it does not count.

  3. Task 3

    Guard it: new branches push themselves

    Work that is only on one laptop can be lost with one forced delete. So make new branches easy to push.

    1. Make a new branch with a commit, and run git push with no other words. Read the error.

    2. Find the Git setting that sets the upstream of a new branch for you.

    3. Turn it on in this repository's config or in your global config.

    To pass: the Check makes a new branch with a commit in a copy of your repository. It runs git push with no other words, and GitHub must then have the new branch.

What you will learn

  • See what happened: find festive-menu's last commit
  • Recover: festive-menu back, here and on GitHub
  • Guard it: new branches push themselves

More in Lost and found

Build "A deleted branch with unmerged work" yourself

Bunty is Tapri's new junior developer, and Kabir keeps handing you the Git incidents left behind. Each is a disaster interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.