Git Advanced Tutorial: Rebasing, Cherry-Pick, and More

Git Advanced Tutorial: Rebasing, Cherry-Pick, and More

By Elena Rodriguez, Developer Experience Editorial Desk · January 16, 2026 · Updated November 7, 2026 · 2 min read

Updated November 7, 2026: Added links to the official Git manual for each command and two new sections on safe history rewriting, bisect automation, stash and reflog expiry.

Quick Answer

The core advanced Git techniques are interactive rebase to clean up history before merging, cherry-pick to apply specific commits to other branches, bisect to find the commit that introduced a bug, stash to save uncommitted work, and reflog to recover lost commits and branches. Never rebase commits that others have pulled.

Introduction

Basic Git commands (add, commit, push) handle daily work. Advanced Git techniques solve complex problems and maintain clean, professional repositories.

Interactive Rebase

Interactive rebase rewrites commit history. Use it to clean up before merging to main.

Run: git rebase -i HEAD~5

Interactive Commands

  • pick - Keep commit as-is
  • reword - Change commit message
  • squash - Combine with previous commit
  • fixup - Squash and discard message
  • drop - Remove commit

Safety Rules

  • Never rebase shared/pushed commits
  • Use --force-with-lease if you must push rebased branches
  • Always have a backup branch

Cherry-Pick

Apply specific commits from one branch to another.

Run: git cherry-pick abc123

Use Cases

  • Backporting bug fixes to release branches
  • Pulling specific features without full merge
  • Recovering commits from deleted branches

How Do You Find Which Commit Introduced a Bug?

Find which commit introduced a bug using binary search.

  1. Start: git bisect start
  2. Mark current (buggy) as bad: git bisect bad
  3. Mark known good commit: git bisect good abc123
  4. Git checks out middle commit
  5. Test and mark good or bad
  6. Repeat until found
  7. Reset: git bisect reset

Git Stash

Save uncommitted changes temporarily.

  • git stash - Save changes
  • git stash list - List stashes
  • git stash pop - Apply and remove
  • git stash apply - Apply and keep
  • git stash -u - Include untracked files

Git Reflog

Reflog tracks all HEAD changes—your safety net.

Run: git reflog

Recovery Examples

  • Undo accidental reset: git reset --hard HEAD@{1}
  • Recover deleted branch: git checkout -b recovered HEAD@{5}
  • Find lost commits: git reflog | grep commit

Rewriting History Without Hurting Your Team

Interactive rebase is the tool for tidying a branch before review. Git's manual describes it as a way to reorder or combine commits, which is exactly what a cleanup pass needs: squash the "fix typo" commits into the change they belong to, reword vague messages, and drop experiments that never mattered. If you commit small fixes as you go, you can mark them for later with a fixup! message and let autosquash fold them into the right commits automatically when you rebase.

The risk is that rebasing creates new commits rather than moving old ones. The Git documentation is blunt about the consequence: rebasing a branch that others have based work on is a bad idea, because everyone downstream has to repair their history by hand. A practical rule is to rebase only branches that exist on your machine, or on a remote branch that nobody else has pulled. When you do need to publish a rebased branch, prefer --force-with-lease over a plain force push. According to the push manual, the option only overrides the remote when it still holds the value you expect, so you will not silently overwrite a teammate's newer commit.

Cherry-pick follows the same logic. It applies the change each commit introduces and records a new commit for each one, so the copy has a different hash from the original. When you backport a fix to a release branch, add the -x flag: Git then appends a line to the commit message to indicate which commit the change was cherry-picked from, which makes later audits much easier.

Letting Git Do the Searching and the Remembering

Bisect is most useful when you stop answering its questions by hand. If you have a script that exits with success for a good build and failure for a bad one, you can bisect with `git bisect run` and let Git walk the history unattended. A short test script that reproduces the bug is usually all it takes, and the result is the first bad commit, ready to inspect. When a midpoint commit cannot be built or tested, use git bisect skip instead of guessing. Be aware that skipping a commit next to the culprit leaves Git unable to say which of them was the first bad one, so skip only when you must. Finish with git bisect reset to return to where you started.

Stash and reflog cover the other half of daily Git work: not losing things. Stash is designed for the moment you want to go back to a clean working directory without committing, for example before switching branches to review someone else's work. Remember the difference between the two ways to bring a stash back. git stash pop will remove the stashed state from the stash list and apply it, whereas git stash apply leaves the entry in place. Add -u when new files matter, because with that option untracked files are also stashed and then cleaned up.

Reflog is your last line of defence after a mistake, since it keeps a local record of when branch tips and other references were updated. It is not permanent, though. Git removes old reflog entries once they pass the configured expiry, and entries for commits that are no longer reachable expire on their own schedule. If you realise you have lost work, check the reflog promptly rather than weeks later, and create a branch at the recovered commit so it is reachable again.

Conclusion

Advanced Git techniques take practice but dramatically improve your workflow. Start with interactive rebase and stash—they solve daily problems. Add bisect and reflog when you need them. Master these tools and you will handle any Git situation confidently.

Sources

The primary sources this article cites, checked in November 2026:

Key Takeaways

  • Interactive rebase cleans up messy commit history
  • Cherry-pick applies specific commits to other branches
  • Bisect finds which commit introduced a bug
  • Reflog recovers lost commits and branches
  • Stash saves work-in-progress without committing

Frequently Asked Questions

Should I rebase or merge?

Rebase feature branches to keep a clean history, ideally cleaning up with interactive rebase before merging to main. Merge shared branches, or whenever preserving branch history matters. Never rebase commits others have pulled, and if you must push a rebased branch, use --force-with-lease and keep a backup branch.

How do I undo a rebase?

Use git reflog to find the commit your branch pointed to before the rebase, then run git reset --hard to that commit. Reflog records every HEAD change; after an accidental reset, git reset --hard HEAD@{1} returns you to the previous position. Keeping a backup branch before rebasing makes recovery simpler.

What does git bisect do?

Git bisect helps you find the exact commit that introduced a bug by running a binary search through your history. You mark a known good commit and a known bad one, then bisect checks out midpoints for you to test until it isolates the culprit. It turns a tedious hunt across many commits into a fast, systematic process.

How can I recover commits or branches I thought were lost?

The reflog is your recovery tool, since it records where your branch references have pointed over time. Even after a bad rebase, hard reset, or deleted branch, you can usually find the lost commit hash in the reflog and restore it. For a deleted branch, git checkout -b recovered HEAD@{5} recreates it from the matching entry.

When should I use git stash instead of committing?

Use git stash to set aside uncommitted work without creating a commit, for example before switching branches. Running git stash pop reapplies the changes and removes the stash, git stash apply reapplies but keeps it, and git stash -u also saves untracked files. This avoids cluttering history with half-finished commits.

About the Author

Elena Rodriguez avatar

Elena Rodriguez

Developer Experience Editorial Desk

Developer Experience Editorial Desk · Web3AIBlog

Elena Rodriguez is a pen name for our developer-experience editorial desk. Posts under this byline are written and reviewed by working engineers covering full-stack development, Web3 dApp architecture, deployment workflows, build tooling, and developer productivity. The desk specializes in turning real production debugging — failed deploys, flaky tests, memory leaks, broken migrations — into reproducible field manuals. Code samples in our tutorials are built from and verified against the official SDKs and documentation, with library versions pinned, before publication.