TekOnline

📚 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:

  1. Make a backup branch.
  2. Check out the old commit in detached HEAD to test.
  3. Make prod point to that old commit.
  4. 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.


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *