Once you're comfortable with branches and merges, a second set of tools lets you shape history deliberately: rebase to move or tidy commits, cherry-pick to copy a single fix to another branch, stash to set aside unfinished work, and tags to mark releases. These commands are powerful because some of them rewrite history, so this lesson pairs each with the rule for using it safely, and shows real output from a small project.
Before you start
You should understand merging from lesson 4 and, importantly, the undo tools and the reflog from Undoing Mistakes in Git: Restore, Reset, Revert, and Reflog. If a rebase goes wrong, the reflog gets you back.
Rebase: replaying commits on a new base
Suppose you branched feature/search from main, made two commits, and meanwhile someone added a commit to main:
* c468f5b Add search results
* 12aae0b Add search box
| * dd31d65 Add menu
|/
* 7638979 Add baseMerging main into the feature branch would add a merge commit. Rebasing instead replays your commits on top of the current main, as if you'd started your work there:
$ git switch feature/search
$ git rebase main
Successfully rebased and updated refs/heads/feature/search.* 2e455c6 Add search results
* a816b2e Add search box
* dd31d65 Add menu
* 7638979 Add baseHistory is now a straight line. Notice the IDs changed: 12aae0b became a816b2e. Rebasing doesn't move commits; it creates new commits with the same changes and a different parent, and moves the branch to them. The originals still exist, reachable through the reflog.
If a replayed commit conflicts, Git stops, you resolve the files as in lesson 4, then git add them and run git rebase --continue. To give up and return to where you started, run git rebase --abort.
The golden rule of rebasing
Don't rebase commits that others have based work on. Because rebasing replaces commits with new ones, anyone who already has the old commits now has a diverged history, and sorting that out is painful. In practice:
- Rebasing your own local or personal feature branch onto the latest
main: fine. - Rebasing
main, or any shared branch: don't.
After rebasing a branch you'd already pushed (your own PR branch, for example), a normal push is rejected because the history changed. Push with:
git push --force-with-leaseUnlike --force, --force-with-lease refuses if the remote branch has commits you haven't seen, so you can't silently delete a collaborator's push. Adding --force-if-includes makes the check stricter.
Merge or rebase?
Both integrate changes; they differ in the history they leave:
- Merge preserves exactly what happened, including when branches split and joined. It never rewrites anything, so it's always safe on shared branches. History can become busy.
- Rebase produces a linear, readable history, but rewrites commits and hides when work actually happened.
A common combination: rebase your own branch onto main to keep it current and tidy, then integrate it into main with a merge or squash through a pull request.
Interactive rebase: tidying your commits
Before opening a pull request, you may want to clean up work-in-progress commits. git rebase -i main opens a list of your branch's commits in your editor, with an instruction per line:
pick 12aae0b Add search box
pick 3f1e2d0 WIP
pick c468f5b Add search resultsChange pick to:
reword(orr) to change a message;squash(s) to combine a commit with the one above, merging messages;fixup(f) to combine it and discard its message;drop(d) to remove it;
or reorder the lines. Save and close, and Git replays the commits accordingly.
A convenient habit: when you notice a small fix belongs in an earlier commit, commit it with --fixup, and let --autosquash place it automatically:
$ git commit -a --fixup HEAD~1
$ git log --oneline -3
a21d15a fixup! Add search box
ea5186c Add search results
5a943d5 Add search box
$ git rebase -i --autosquash main # the fixup is already moved and marked; just save
$ git log --oneline -3
e892e76 Add search results
ad64dd9 Add search box
09a1669 Add baseThe fix is now part of "Add search box", and reviewers see two clean commits.
Cherry-pick: copying one commit
git cherry-pick <commit> applies the change from one commit onto the current branch, as a new commit. Its classic use is delivering a fix to a release branch (see release branches in lesson 8):
$ git switch release/1.x
$ git cherry-pick -x d8a84fa
[release/1.x 280fcc3] Fix crash on empty menu
$ git log -1 --format=%B
Fix crash on empty menu
(cherry picked from commit d8a84fa37e5a2421bdca2b2333934f9a6e54469d)The -x option adds the "cherry picked from" line, so anyone can trace the copy back to the original. Use it whenever you cherry-pick between public branches.
Cherry-pick copies changes, not history, so the same change now exists as two commits. That's fine for targeted fixes; it's a poor substitute for merging whole branches, which would leave you tracking dozens of duplicates.
Stash: setting work aside
You're halfway through a change when an urgent bug report arrives. Your work isn't ready to commit, and you can't switch branches with it in the way. git stash saves uncommitted changes and cleans your working tree:
$ git stash push -m "half-done filter"
Saved working directory and index state On feature/search: half-done filter
$ git stash list
stash@{0}: On feature/search: half-done filterFix the bug on another branch, come back, and restore your work:
git switch feature/search
git stash pop # reapply the latest stash and remove it from the listUseful variations: git stash push -u also stashes untracked files; git stash apply reapplies without removing the stash; git stash show -p stash@{0} shows what's inside; git stash drop deletes one.
Stashes are easy to forget. They're meant for minutes or hours, not weeks. For anything longer, commit to a work-in-progress branch instead; it's visible, pushable, and backed up.
Tags and releases
A tag is a name for a specific commit that, unlike a branch, doesn't move. Tags mark releases, so v1.2.0 always means the same code.
Git has two kinds:
- Lightweight tags are just a name pointing at a commit:
git tag v1.2.0-test. - Annotated tags are full objects with a tagger, date, and message, and can be signed. Use annotated tags for releases.
git tag -a v1.2.0 -m "Release 1.2.0"
git push origin v1.2.0 # tags aren't pushed by defaultgit describe names any commit relative to the nearest tag, which is handy for build version strings:
$ git describe
v1.2.0
# one commit later:
$ git describe
v1.2.0-1-g8288f3bThat reads "one commit after v1.2.0, at commit 8288f3b."
Version numbers
Many projects use Semantic Versioning: MAJOR.MINOR.PATCH. Increase PATCH for backward-compatible bug fixes, MINOR for backward-compatible new features, and MAJOR for incompatible changes. It tells users at a glance whether an upgrade might break them.
From tag to release
Hosting platforms turn tags into releases: a page with release notes and downloadable files. A typical release process:
- Make sure
main(or the release branch) is green in CI. - Update the changelog with user-facing changes since the last tag.
- Create and push an annotated, ideally signed, tag (lesson 12).
- Let CI build the artifacts from the tag and publish the release (lesson 11).
Never move or delete a published tag. If a release is broken, publish a new version.
Common mistakes and troubleshooting
- Rebasing a shared branch. Teammates end up with diverged history. Rebase only your own branches.
- Using
--forceinstead of--force-with-lease. It can delete others' commits. - Abandoning a rebase halfway through. Run
git status; it says a rebase is in progress. Continue orgit rebase --abort. - Forgetting stashes. Run
git stash listnow and then; prefer WIP branches for longer pauses. - Cherry-picking whole features. Merge branches instead; cherry-pick is for targeted fixes.
- Tags not on the remote. Push them explicitly:
git push origin v1.2.0. - "Rebase lost my work." Find the pre-rebase commit in
git reflogand reset to it.
Practical exercise: prepare a clean release
In a practice repository:
- Create
feature/exportfrommainwith three commits, one of them a "WIP" commit. Add a commit tomainmeanwhile. - Rebase the feature branch onto
main, then use interactive rebase to squash the WIP commit and reword one message. - Make a small fix with
--fixupand apply it with--autosquash. - Merge the branch into
mainand tag itv1.0.0with an annotated tag. - Create
release/1.xfrom the tag. Commit a bug fix onmainand cherry-pick it with-xtorelease/1.x; tag thatv1.0.1. - While doing step 5, practice stashing an unfinished change and restoring it.
Key takeaways
- Rebase replays commits onto a new base, creating new commits; it gives linear history but rewrites it.
- Never rebase shared history; after rebasing your own pushed branch, use
--force-with-lease. - Interactive rebase,
--fixup, and--autosquashtidy commits before review. - Cherry-pick with
-xcopies targeted fixes between branches. - Stash briefly; use WIP branches for longer pauses.
- Annotated tags mark releases; follow a versioning scheme and never move published tags.
References
- Pro Git: Rebasing (opens in a new tab)
- Pro Git: Rewriting History (opens in a new tab)
- Pro Git: Stashing and Cleaning (opens in a new tab)
- Pro Git: Tagging (opens in a new tab)
- Git documentation: git cherry-pick (opens in a new tab)
- Git documentation: git push (force-with-lease) (opens in a new tab)
- Semantic Versioning (opens in a new tab)
What's next
You've finished the Intermediate level. The Pro level starts by automating what you've been doing by hand: CI/CD with Git: GitHub Actions, GitLab CI/CD, and Open-Source Alternatives.

