Search code examples
gitmethodology

Coding methodology : keeping the good things while trying new ones


I think this question is very general, so sorry for not being specific. When I modify some come code to try something new, whatever the project or language, I may have to revert back to previous Git commits if it all fails. But in the process of making these uncertain modifications (let's call them X), I also make some nice corrections or additions (let's call them Y) that I want to keep.

Is there a clever way to separate X and Y, so that at the end of the day I can keep Y and discard X?


Solution

  • Here's what I do:

    1. Make a new branch for my "uncertain modifications".
    2. As I'm going, make small commits that can stand alone.
    3. If my commits aren't small enough to keep only the changes I need, I'll have to go back and split old commits. Interactive rebase to the commit in question, reset to HEAD~, and split the changes into relevant commits.
    4. Rebase/cherry-pick commits I want to keep back onto my original branch.
    5. Squash those small commits to keep history clean.