← Git & GitHub from Zero
Rescue tools: stash, cherry-pick, reflog, reset and rebase

Lesson 6 of 7

Rescue tools: stash, cherry-pick, reflog, reset and rebase

Park unfinished work with stash, copy one commit with cherry-pick, find lost commits with reflog, move a branch back with reset, and tidy history with rebase.

The idea in plain words

Stash is a drawer: you sweep half-finished work into it and get a clean desk. Cherry-pick is copying one page from another notebook without taking the whole notebook. Reflog is a security camera that remembers everywhere you have been, even after you moved on.

Reset is rewinding a tape to an earlier point, and rebase is lifting your pages off and putting them back on top of the newest version. The one rule to remember is to only rewrite history that is still yours.

Commands in this lesson

git stash
Put unfinished edits in a drawer and get a clean working folder. Bring them back with git stash pop.
git cherry-pick <commit>
Copy one commit onto your current branch. It gets a new hash.
git reflog
List everywhere HEAD has been, including commits that look lost.
git reset --soft | --mixed | --hard
Move your branch back. Soft keeps changes staged, mixed unstages them, hard discards them.
git rebase <branch>
Replay your commits on top of another branch for a straight history.

Written notes from the video

Why rescue tools

Everyone gets into a mess with Git at some point. This is Git and GitHub from zero, episode six: rescue tools. Five commands that get you out of trouble, and one rule to keep you safe.

Real-life analogies

Here are five rescue tools, in everyday terms. Stash is a drawer. You sweep your half-finished work into it, and get a clean desk. Cherry-pick is copying one page from another notebook, without taking the whole notebook. Reflog is a security camera. It remembers everywhere you have been, even after you moved on. Reset is rewinding a tape to an earlier point. Rebase is lifting your pages off, and putting them back on top of the newest version.

Command 1: git stash

Command one: git stash. You are halfway through a change, and an urgent fix comes in. You are not ready to commit. git stash puts your unfinished edits in a drawer, and gives you a clean working folder. Fix the urgent thing. Then run git stash pop to bring your edits back. git stash list shows everything in the drawer.

Command 2: git cherry-pick

Command two: git cherry-pick. Say a useful commit lives on another branch, and you want just that one. Find its hash with git log, then run git cherry-pick and the hash. Git copies that single commit onto your current branch. It gets a new hash, because it is a new commit.

Command 3: git reflog

Command three: git reflog. git log shows the commits your branch can reach. Reflog shows everywhere HEAD has been. That includes commits that look lost, after a bad reset, or a deleted branch. Every entry has a name like HEAD@{2}. That is your way back. Entries are kept for around ninety days by default.

Command 4: git reset

Command four: git reset. Reset moves your branch back to an earlier commit. There are three modes. Soft keeps your changes staged in the box. Mixed, the default, moves them back to your desk. Hard throws them away completely. Say you committed too early. git reset --soft HEAD~1 undoes the commit, and keeps your work. Hard is dangerous. Only use it on commits you have not pushed, and only when you are sure. And if you reset too far, the reflog can usually bring commits back.

Command 5: git rebase

Command five: git rebase. Your feature branch started from an older version of main. Main has moved on since. git rebase main lifts your commits, and replays them on top of the newest main. The result is a clean, straight history, as if you had started today. The golden rule: never rebase commits that other people already have. Rebase changes commit hashes.

Recap

Recap. Stash parks unfinished work. Cherry-pick copies one commit. Reflog remembers where you have been. Reset moves a branch back. Rebase replays your work on top of the latest. And the rule: only rewrite history that is still yours.

Next episode

Next episode, the last one: good habits on GitHub. Subscribe so you do not miss it, and find written notes for every lesson at changecv.com/skills.

Common questions

Can I get a commit back after git reset --hard?

Usually yes, if the work was committed. Run git reflog to find the commit and move back to it with git reset --hard and its hash. Edits that were never committed cannot be recovered after a hard reset.

What is the difference between reset --soft, --mixed and --hard?

All three move your branch back. Soft keeps the undone changes staged, mixed (the default) keeps them in your working folder unstaged, and hard throws them away.

When should I not use git rebase?

Never rebase commits that other people already have, such as commits you pushed to a shared branch. Rebase gives commits new hashes, which causes confusion for anyone who built on the old ones.

Does git stash save new files?

Not by default. git stash saves changes to tracked files. Add the -u flag to include untracked files as well.

What is the difference between cherry-pick and merge?

git merge brings in every commit from another branch. git cherry-pick copies only the commits you name, each as a new commit.