This guide shows how to take the top commit from your current branch, temporarily stash it, update your branch to match the latest main, and then re-apply the stashed changes.
You’ve made one local commit on a feature branch but later realize you want that commit to apply on top of the latest main instead. Rather than rebasing or cherry-picking, you want to:
- Temporarily stash your top commit.
- Sync your branch with the latest
main. - Apply the changes back on top of
main.
# Move HEAD back one commit, but keep the changes staged
git reset --soft HEAD~1
# Stash the staged changes
git stash push -m "Top commit stash" --keep-index✅ This saves your latest commit as a stash entry, and leaves your working tree clean.
# Fetch the latest changes from remote
git fetch origin
# Hard reset your branch to match remote main
git reset --hard origin/main
⚠️ Warning: This will remove any uncommitted changes. Make sure everything is stashed or committed.
# Apply the stash
git stash pop🧹 This applies the stashed commit and removes it from the stash list.
If you want to see the stashes before popping:
git stash list
git stash show -p stash@{0}To apply a specific stash instead of popping:
git stash apply stash@{0}If you prefer to avoid a hard reset (for safety):
git stash push -m "Top commit stash"
git fetch origin
git reset --merge origin/main
git stash pop✅
--mergewill preserve local changes unless there are conflicts.
You now have your latest changes re-applied on top of the most recent main, as if you had committed them freshly after syncing with the base branch.