Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sandeepkv93/450a4675c10679032f8608b57d30d374 to your computer and use it in GitHub Desktop.
Save sandeepkv93/450a4675c10679032f8608b57d30d374 to your computer and use it in GitHub Desktop.

βœ… Use Case: Apply Only Top Commit from a Branch on Top of main

🎯 Goal

From a feature branch, apply only the latest commit on top of the latest main branch β€” and discard all earlier commits in that branch.


πŸ“Œ Assumptions

  • Your feature branch is named: my-feature
  • You want to keep only the most recent commit from my-feature
  • You want it based on the latest origin/main

🧾 Steps

# 1. Check out your feature branch
git checkout my-feature

# 2. Reset the branch to match origin/main, but keep the latest commit's changes
git reset --soft origin/main

# 3. Re-commit the top commit changes on top of main
git commit -m "Your top commit message"

# (Optional) Verify the history
git log --oneline --graph --decorate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment