Module 2 · Git: the 20 everyday commands · Lesson 14 of 16

Undo safely: reset, revert, checkout and tag

Unstage files, drop local commits, reverse shared ones, throw away edits, and mark releases.

In this lesson · about 24 minutes

  • Unstage a file with git reset FILE
  • Drop commits that were never pushed with git reset (and know --soft, --mixed, --hard)
  • Undo a commit others already have with git revert
  • Discard edits to a file with git checkout -- FILE, and mark releases with git tag

Key terms

Say the meaning to yourself, then tap a card to check.

Sorting drill

reset or revert?

Has anyone else got the commit? That decides it.

  • Undo a commit you made 5 minutes ago and have not pushed
  • Undo a commit that is on GitHub and your team has pulled
  • Take a file off the staging area
  • Back out a bad release that is already live

0 of 4 sorted

Practice terminal · missions

A messy morning: clean it up without losing anything

Sam accidentally staged a secrets file, has a half-done holiday banner, an unpushed debug commit, and an already-pushed commit that switched prices to euros by mistake. Fix it all.

Missions 0 of 13

  1. 1Look at the status.
  2. 2Take secrets.txt off the staging area.
  3. 3Delete secrets.txt from the folder.
  4. 4Park the unfinished banner work in the stash.
  5. 5Look at the history, one line each.
  6. 6Drop the unpushed debug commit completely.
  7. 7Undo the pushed 'Switch prices to euros' commit the safe way.
  8. 8Bring the banner work back out of the stash.
  9. 9Oops: echo "junk" > README.md (this overwrites the file!)
  10. 10Throw away the change to README.md.
  11. 11Commit the finished banner.
  12. 12Tag this commit as release v1.1.
  13. 13Push your commits and your tags.
sam@practice-box:~/website

Type a command and press Enter. Try help to list commands. Tab finishes names, ↑ repeats.

Repository ~/website · on main

1 · Working folder

The files you edit

  • README.mdsaved
  • index.htmlchanged
  • prices.txtsaved
  • secrets.txtsaved
  • style.csssaved

2 · Staging area

Packed for the next commit

  • secrets.txtnew

3 · Commit history (this computer)

Each dot is a saved snapshot. Labels are branches, tags and what you last heard from origin.

cbc60baHEAD → mainWIP debug lines
a847c43origin/mainSwitch prices to euros
17743daAdd price list
b644ba3Add phone number to footer
534d516Add styles
dd28f37Add home page

☁ Remote: origin

https://github.example/sam/website.git

  • maina847c43Switch prices to eurosbehind you

Check your understanding0 of 4 correct

Question 1

1.You staged config.local by accident. Which command takes it off the stage but keeps your file?

Question 2

2.A bad commit is already on GitHub and teammates have pulled it. What do you use?

Question 3

3.Which reset throws away uncommitted changes in your files?

Question 4

4.You just tagged v2.0 and pushed. Your colleague cannot see the tag. Why?

Quiz score: 0 of 4

Tip: answer every quiz question correctly first.

Written by Noviq Academy in its own words. The practice terminal is a simulation that runs in your browser: it imitates Linux (Ubuntu-style output) and Git closely, but it is not a real computer, and real output can differ slightly. The servers, people, web addresses and GitHub addresses in the labs are made up. Linux is a trademark of Linus Torvalds; Git and GitHub belong to their owners. Read-aloud uses your device's built-in voices.