Skip to content

Instantly share code, notes, and snippets.

@gidhon
Last active December 5, 2023 11:17
Show Gist options
  • Save gidhon/3e38fdbf681adb7199a1 to your computer and use it in GitHub Desktop.
Save gidhon/3e38fdbf681adb7199a1 to your computer and use it in GitHub Desktop.
Creating a new branch to discard commit history

Discard commits while saving changes

Scenario

You have made some commits, pushed it upstream to the remote branch, then realized that some or all of those commits must for some reason not be included. In example, accidently commiting large multimedia files (blobs) that will forever bloat the commit history of the branch.

  1. ensure all changes are commited in your current, "faulty" branch
  2. switch to your master branch
    • git checkout master
  3. update master with latest changes on remote
    • git pull origin master
  4. create a new branch and check in to it
    • git checkout -b <new-branch-name-squash>
  5. ensure you're in the new branch
    • git branch
  6. merge changes from "faulty" old branch into this new branch
    • git merge <old-faulty-branch> --squash
  7. this will merge all the changes from the old branch into the new one while ignoring the commit history
  8. do a status update to see what's cooking
    • git status
  9. you may now re-stash/add, starting with a clean slate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment