Practical Git Tips: Rebase, Cherry‑pick, Stash and Common Scenarios
This article walks through eight typical Git problems—missing branches, unwanted merges, unfinished work, wrong commit messages, early‑stage fixes, mass commit squashing, and recovery after mistakes—showing how to use rebase, cherry‑pick, stash, revert and related commands to resolve them efficiently.
Most Git tutorials teach git add and git push , while commands like git rebase are rarely covered; many developers are warned that rebase is dangerous and stick to git merge . This guide explains rebase and also dives into git cherry-pick , git stash , and other useful Git operations.
Eight common situations are presented to test your Git knowledge:
Accidentally committing a new feature to the wrong branch.
Merging a feature into master only to be told the feature is no longer needed.
Needing to pull the latest master while you have uncommitted work.
Realizing a recent commit has an incorrect message.
Being asked to modify an earlier commit after a code review.
Multiple commits with wrong messages that need fixing.
Consolidating dozens of commits into one for a clean review.
Correcting a commit message that is already on master with many subsequent commits.
Problem 1 – Wrong Branch Commit : Use git cherry-pick to copy the desired commit(s) to the correct branch without affecting the original branch. Example: git cherry-pick 123abc 456def 789ghi . If the cherry‑pick results in an empty commit, you can skip it with git cherry-pick --skip or abort with git cherry-pick --abort .
Problem 2 – Unwanted Feature Merge : Revert the merge using git revert with the appropriate commit range, e.g., git revert abcdef123..7890abcd , to create a new commit that undoes the changes.
Problem 3 – Pulling master Without Committing : Stash your current changes with git stash , update from master , then restore the work with git stash apply (or git stash pop to drop the stash). Use git stash list to view saved stashes and specify a particular stash if needed.
Problem 4 – Wrong Commit Message : Amend the most recent commit with git commit --amend . This opens the editor (often Vim); edit the message, then save and quit (e.g., Esc :wq ).
Problem 5 – Fixing an Earlier Commit : Run an interactive rebase starting before the target commit, e.g., git rebase -i <hash_of_commit_before_target> . Change the command from pick to edit for the commit you want to modify, make the code changes, stage them, and then use git commit --amend followed by git rebase --continue . The interactive rebase supports many commands ( pick , reword , edit , squash , fixup , drop , etc.).
Problem 6 – Multiple Wrong Messages : Use the same interactive rebase approach, selecting reword or edit for each problematic commit, or simply revert and recommit if the history is already public.
Problem 7 – Squashing Many Commits : Create a merge request to view the diff, then cherry‑pick the relevant commits onto a new branch and run git rebase -i with squash to combine them into a single commit before merging.
Problem 8 – Fixing a Message on master : Because the commit is already on a shared branch, the safest approach is to git revert the faulty commit and create a new commit with the correct message, avoiding a history rewrite that would affect many downstream commits.
Recovery Tips : If a rebase or cherry‑pick goes wrong, abort with git rebase --abort or git cherry-pick --abort . Use git reflog to locate previous HEAD positions and reset with git reset --hard <hash> (or --soft / --mixed for safer resets).
Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.