Skip to content

Advanced Git commands: stash, cherry-pick, revert, reset #12

Description

@d8603276-hue

Summary### SummarySummarySummary

This issue documents several advanced Git commands and practical usage examples so contributors can quickly reference how to use them: git stash, git cherry-pick, git revert, and git reset. Each section includes a concise explanation, common options, safety notes, and copy‑paste examples.

Scope

  • Audience: contributors and maintainers who need a quick reference for advanced Git workflows.
  • Goal: make it safe and easy to apply, revert, or move commits and manage local work in progress.

Content

  1. git stash

What it does

  • Temporarily saves uncommitted changes (working tree and index) so you can switch branches or pull without committing unfinished work.

Common commands

  • Save changes:
    • git stash push -m "WIP: "
  • List stashes:
    • git stash list
  • Apply stash (keep entry):
    • git stash apply stash@{0}
  • Apply and remove:
    • git stash pop stash@{0}
  • Show stash diff:
    • git stash show -p stash@{0}
  • Drop a stash:
    • git stash drop stash@{0}
  • Create a branch from stash:
    • git stash branch feature-from-stash stash@{0}

Safety notes

  • Use descriptive messages with -m when you have multiple WIPs.
  • git stash pop removes the stash even if apply fails; use git stash apply to keep it until successful.
  1. git cherry-pick

What it does

  • Copies the changes introduced by an existing commit (or commits) onto your current branch as new commits.

Common commands

  • Pick a single commit:
    • git cherry-pick
  • Pick multiple commits:
    • git cherry-pick sha1 sha2 sha3
  • Pick a range (inclusive):
    • git cherry-pick shaA..shaB
  • Add original commit reference in message:
    • git cherry-pick -x
  • Apply without committing (stage changes only):
    • git cherry-pick -n

Safety notes

  • Cherry-picking may cause conflicts; resolve them and run git cherry-pick --continue.
  • Use -x for traceability when bringing commits between branches.
  1. git revert

What it does

  • Creates a new commit that undoes the changes from a specified commit. Safe for public branches because it preserves history.

Common commands

  • Revert a single commit:
    • git revert
  • Revert multiple commits (newer to older):
    • git revert sha_newer sha_older
  • Revert without editor:
    • git revert --no-edit

Safety notes

  • git revert appends a new commit that negates the target commit; resolve conflicts and run git revert --continue if needed.
  1. git reset

What it does

  • Moves HEAD to a specified commit and optionally updates the index and working tree. It can rewrite history—use with caution on shared branches.

Modes and examples

  • Soft (move HEAD, keep index & working tree — changes staged):
    • git reset --soft HEAD~1
  • Mixed (default — move HEAD, reset index, keep working tree — changes unstaged):
    • git reset --mixed HEAD~1
  • Hard (move HEAD, reset index & working tree — discards changes):
    • git reset --hard HEAD~1

Other examples

  • Unstage a file but keep changes:
    • git reset HEAD -- path/to/file
  • Reset to remote branch state (dangerous):
    • git fetch origin
    • git reset --hard origin/main

Safety notes

  • Avoid git reset --hard on shared branches; prefer git revert for undoing public commits.

Quick reference

  • Keep local WIP while switching tasks: git stash
  • Bring a commit from another branch: git cherry-pick
  • Safely undo a public commit: git revert
  • Rewrite history / move HEAD locally: git reset

Examples (copy/paste)

Stash, switch branch, apply stash

git stash push -m "WIP: tweak formatting"
git checkout other-branch

do work, then return

git checkout feature-branch
git stash pop

Cherry-pick a commit from another branch

git checkout feature-branch
git cherry-pick abc123 # abc123 is commit SHA from another branch

Revert a public commit

git checkout main
git pull
git revert abc123
git push

Reset local branch to drop last commit (keep changes staged)

git reset --soft HEAD~1

Notes about repository and metadata

  • Repository: youthlin/t
  • Suggested label(s): documentation

If you want,

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions