Your cart is currently empty!
๐ How to Reset a Branch to an Old Commit and Bring Back Only the Changes You Want (Even with Binary Files)
๐ A Practical Guide for When You Want a Fresh Base But Can’t Lose Everything
๐ The situation
You’re working on a Git branch โ let’s call it prod
. Over time, it’s picked up all sorts of changes: some good, some bad, maybe some experimental stuff you don’t want anymore.
You decide:
- “I want to start this branch fresh โ using an older, stable commit as my new base โ but I do need to bring over some good changes from the messy history.”
So you:
- Pick an old commit that’s clean.
- Want to resetย
prod
ย to that commit. - Want to keep some changes from the current branch โ so you back them up.
- Then you want to cherry-pick or diff-pick just what you need, one piece at a time.
๐๏ธ The plan
Here’s the safe, reliable plan you’ll follow:
- Make a backup branch.
- Check out the old commit in detached HEAD to test.
- Makeย
prod
ย point to that old commit. - Bring back changes you want โ with:
git cherry-pick
ย for whole commits,- orย
git diff + git apply
ย for partial changes, - andย
git checkout
ย for binary files thatยgit apply
ย can’t handle.
โ Step 1 โ Backup your current branch
First, always save your work before you do destructive things.
# Make sure you're on your branch
git checkout prod
# Make a backup branch
git branch backup/prod
Now you can always get back to where you were.
โ Step 2 โ Pick your new base commit
Find the commit you trust:
git log --oneline
Copy the SHA โ let’s say it’s:
ed69c97b5311ea67efb6fa93efe97b0a4dd2b78a
Check it out in detached HEAD:
git checkout ed69c97b5311ea67efb6fa93efe97b0a4dd2b78a
Build, test โ be sure this is truly the base you want.
โ Step 3 โ Reset your branch to the old commit
When you’re sure:
# Force your branch to point here
git checkout -B prod
What this does:
-B
ย means “create or reset this branch to here”- Nowย
prod
ย is exactly this commit.
โ Step 4 โ Bring back the changes you want
Now you decide how to pull back the changes:
๐ข A) git cherry-pick
for full commits
If your backup branch has clean commits you want as-is:
# Go back to prod
git checkout prod
# See backup commits
git log backup/prod
# Cherry-pick the commit you want
git cherry-pick <sha>
# For multiple:
git cherry-pick <oldest>^..<newest>
๐ข B) git diff + git apply
for partial changes
If you want the difference between your new base and your backup โ but not a full commit replay โ use diff:
# Make sure you're on prod
git checkout prod
# See what changed
git diff HEAD backup/prod
# To stage the diff:
git diff HEAD backup/prod | git apply --index
โ
--index
means “apply and stage it immediately.”
Now git status
shows those files staged โ commit when you like.
๐ข C) Fixing binary files
Problem: git apply
does not handle binary files well. You’ll see:
error: cannot apply binary patch
Solution: Just checkout the file directly:
# Replace the binary file with the version from backup
git checkout backup/prod -- path/to/file
# Stage it
git add path/to/file
โ Step 5 โ Commit
When you’re happy with what’s staged:
git commit -m "Bring back selected changes from backup"
โ Step 6 โ Push with care
Because you force-reset the branch’s history, you’ll probably need to force-push:
git push --force
โ ๏ธ Be sure you coordinate with teammates first โ or push to a new branch โ so you don’t overwrite work they haven’t pulled yet!
โก Bonus: What if you want to see what changed first?
Always preview:
git diff HEAD backup/prod
Or for a specific file:
git diff HEAD backup/prod -- path/to/file
โ Key takeaways
git branch backup/your-branch
ย โ safety net.git checkout -B your-branch
ย โ force-reset branch to any commit.git cherry-pick
ย โ replay commits exactly.git diff | git apply --index
ย โ stage raw diffs instead.git checkout <commit> -- file
ย โ grab individual files (great for binary or big files).
๐ Result
You get a clean branch with only the changes you actually want, built from a known good commit. No weird leftover history, no surprises.
by
Tags:
Leave a Reply