Home > Software design >  In git, how can I "evolve" the following commits in a stack?
In git, how can I "evolve" the following commits in a stack?

Time:03-16

Here's my commit stack:

E (current -- contains error)
D (contains error)
C (contains error)
B (contains error)
A (oldest -- contains error)

There's an error in A, so I fix it, and I want that fix to apply to all the following commits above it in the stack.

I've read about fixup autosquash, but that seems to only apply to a specific commit, and I'd like it to apply to all subsequent commits.

What's the common practice for this type of situation?

CodePudding user response:

Git fixup and rebase autosquash is your way to go. First commit with fixup pointing to the commit that introduced the error (as per your example 'A'):

git commit -a --fixup <commit A hash here>

this will produce a new fixup commit on top of existing ones:

F Fixup! (oldest -- contains error)
E (current -- contains error)
D (contains error)
C (contains error)
B (contains error)
A (oldest -- contains error)

Now to apply the fix to all the subsequent commits you need to do rebase:

git rebase --autosquash --interactive <commit hash before A >

This will call the editor you have set up with list of commits and rebase steps in order:

pick HashA
fixup HashF fixup!
pick HashB 
pick HashC 
pick HashD 
pick HashE 

Once you confirm/close the editor git will apply the changes to all commits after A. Note that all commit hashes will be changed as they are not the same commits anymore.

  •  Tags:  
  • git
  • Related