This time around here’s a simple way to fix a commit in the middle of your history before sharing it. The idea is to correct the existing commit and not to add new commits. (Thanks Aurélien for the idea!)

How it works

Let’s take the example of a working branch, feature / api. I’m not happy with commit B and want to fix it.

  • Create a fix commit from the last commit (HEAD). This commit is a bit special insofar as it is a temporary commit whose message will be created automatically (fixup)

 

  • Start an interactive rebase with the autosquash option. The fix commit will automatically be positioned to be merged with B.

 

  • Finalize the interactive rebase. The fixup commit is gone and my change has been included into B

Of course, since interactive rebase is a history rewrite tool, B commits and all its successors will get new SHA1. Be careful therefore to avoid rewriting an already pushed history (unless you are working alone on this branch).

Commands

Let’s see all this now in commands and from a small example!

1 – Identify the proper commit to be fixed

 

[ennael@zarafa lab-git (devel)]$ git log --oneline  
af522a9 (HEAD -> devel) Add todo list 
700b44e Add isc license file 
6b7c25f Fix title et add more documentation 
f3150b6 Add lib/fic4 library                                         <----- commit à corriger
1d05b4b Add lib/fic3 library

2 – Create the commit that will fix it

The fix will be committed using the –fixup = <SHA1 of the commit to be fixed>

 

[ennael@zarafa lab-git (devel)]$ vi lib/fic4
[ennael@zarafa lab-git (devel)]$ git add lib/fic4
[ennael@zarafa lab-git (devel)]$ git commit --fixup=f3150b6
[devel 5b360c8] fixup! Add lib/fic4 library 
1 file changed, 1 insertion(+)
[ennael@zarafa lab-git (devel)]$ git log --oneline  
5b360c8 (HEAD -> devel) fixup! Add lib/fic4 library
af522a9 Add todo list
700b44e Add isc license file

3 – Start interactive rebase

Do not forget –autosquash option

 

[ennael@zarafa lab-git (devel)]$ git rebase -i --autosquash f3150b6~1

Nothing to do here. Just validate this interactive rebase

 

pick  f3150b6 Add lib/fic4 library
fixup 5b360c8 fixup! Add lib/fic4 library
pick  6b7c25f Fix title et add more documentation
pick  700b44e Add isc license file
pick  af522a9 Add todo list

4 – Tadaaaa here the result!

Voila, my fix has been integrated and my history has been rewritten!

 

 

[ennael@zarafa lab-git (devel)]$ git log --oneline
3eaccfd (HEAD -> devel) Add todo list 
405aa43 Add isc license file 
e5d2b75 Fix title et add more documentation 
c4fa3b5 Add lib/fic4 library 
1d05b4b Add lib/fic3 library

Nice ! Let’s sum up!

 

Check the commit SHA1 to be fixed

git log --oneline

Create and commit your modification in a temporary commit

git commit --fixup=<SHA1 of the commit to be fixed>

Start interactive rebase

git rebase -i --autosquash <SHA1 of the commit to be fixed>~1 

Validate

 

Use autosquash option by default with interactive rebase

git config --global rebase.autoSquash true