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 *