Git without fear · Chapter 14 of 28
Oops, that was main
By the end of this chapter you can move commits made on the wrong branch onto a branch of their own, and put main back where it belongs. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Branches
- 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
Bunty used your laptop while you were getting chai. "Monsoon specials, done! Adrak chai, onion pakora, prices and all." Two commits. Kabir glances at the prompt: (main).
New work goes on a branch, so the team can review it before it reaches main, and these two commits are not ready for the app. The good news: nothing is pushed, so GitHub's main is untouched.
What you know so far does not quite fit. git revert would add two more commits undoing these, and the monsoon work would stay buried in main's history under its own undo. git reset on its own moves main back but leaves the commits with no name, and you would be digging them out of the reflog (chapter 11). Copying the files to a new branch by hand loses Bunty's commits and their messages. You need the commits kept and main moved.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Keep the monsoon commits on feature/monsoon-menu
Bunty made two monsoon commits on main by mistake. First keep them safe on a new branch.
1. Run git log --oneline --all. Bunty's two commits are on main.
2. Make a branch called feature/monsoon-menu where you are now. You do not have to switch to it.
3. Do this before you move main.
To pass: feature/monsoon-menu has both commits, "Add the monsoon specials" and "Price the monsoon specials". It has monsoon.txt (with Onion pakora 60) and the monsoon prices in prices.csv.
- Task 2
Move main back to GitHub's main
Now move main back to where GitHub's main is.
1. Make sure the new branch exists first (task 1).
2. Move main to origin/main with git reset. Use the mode that also changes your files.
3. Do not push.
To pass: main is on the same commit as origin/main, so its commits are only "Add the price list" and "Add the menu". GitHub's main and origin/main have not changed.
- Task 3
Finish on main, with a clean folder
Finish on main, with a clean folder.
1. Stay on main.
2. Run git status. If files are staged or changed, make them match main's commit.
To pass: you are on main, and main is on the same commit as origin/main. There is no monsoon.txt in the chapter folder. git status shows nothing staged or changed, except answer.txt.
- Task 4 · interview question
Interview: what a branch really is
Interview question: what is a branch, really?
1. Think about this chapter. No commit was copied or changed. Only names moved.
2. Write one line that says what a branch is. Use the word commit.
3. Save it in answer.txt, in the chapter folder.
To pass: answer.txt says what a branch is, with the word commit and a word like points, pointer or reference.
What you will learn
- origin/main: your copy of where GitHub's main was the last time you talked to it.
- git branch with a name: a new branch on the commit you are on, without switching to it.
- git reset --hard origin/main: moving main back to match GitHub, files and all.
More in Branches
Build "Oops, that was main" 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.