Git, What is the recommended applying bug changes in a older commit


I have a code public repository with the following structure, new features are implemented by breaking off a new branch from master and merging it back.

     /          (master)
    /              |

Lets say there is a bug in the commit which production is pointing to (B).

  1. What is the recommended/best way(s) to apply the bug fix to Production in Git?
  2. Is it possible to apply the bugfix(U) to later features/ commits (C, D, E and so on), if so what recommended way(s) on doing it?
A---B---C'---D'---E' ... J'---K' 
  1. Is it possible to have the commit C point to U ?
     (bugfix)              (master)
        |                     |
A---B---U---C---D---E ... J---K 

if so how do i have the changes applied to C, D, E ..

     (bugfix)                  (master)
        |                         |
A---B---U---C'---D'---E' ... J'---K' 


  1. Clarifying the question
    • I don’t want to merge (U) to master, lets say this is because I don’t want (K) to be in production yet
    • I will be moving production from C, D, E… to K not directly to K


I highly recommend to use online git playground to emulate your process, those emulators include: (disclaimer: I’m not the developer of this site)

Visualized Demo:this process visual demo

Firstly, I think those things could only happened with bad branch management.

If you don’t want K to be in production yet, why would you merge/commit K into the master branch.

To deal with this issue, you can

  1. Checkout and make a new branch(dev) just like current master branch
  2. Back to master branch, and reset head on B
  3. Cherry-pick U onto master branch
  4. Merge/Rebase your new master into your dev branch.
  5. Finally, it may looks like:
     (bugfix, master)
                          (possible merge request commit, dev)
A---B---C---D---E ... J---K---L

Answered By – hide1nbush

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published