Search code examples
git

What is the best way to make the same change to multiple branches?


Say I have two branches. One of them contains a file, and the other contains a slightly modified version of the file. I notice that both files have the same bug in them. Do I have to go into each branch and fix the bug in each version of the file, or is there a way to do this to both files at the same time?


Solution

  • Cherry picking is the way to go for a one-off fix.

    The steps would be:

    1. Make the fix in one branch, and commit the result.
    2. Record (copy to clipboard?) the hash of the commit you just made
    3. Switch to the other branch

      git checkout otherbranch
      
    4. Cherry-pick the commit that you remembered the hash for:

      git cherry-pick COMMIT_SHA
      

      Alternatively, if it's the latest commit for the first branch, you can just put the branch name instead of the hash.