How to create a hotfix

If you follow the Semantic Versioning notation when delivering your code it’s important to know the way to create a hotfix when needed. Here are the basic steps to follow in order to achieve it:

  1. Get the latest changes from your remote
    • git fetch
  2. Checkout the version you would like to patch with the hotfix (ex.: “v1.2.3”)
    • git checkout VERSION
  3. Create the hotfix branch for the related version. It should follow the semver convention (ex.: “v1.2.3-1”)
    • git checkout -b hotfix/HOTFIXVERSION
  4. Do your modifications, cherry-pick, apply patches, etc.
  5. Commit your changes
    • git add .
    • git commit -m “fix: The commit message comes here”
  6. Push them to the remote
    • git push -u origin hotfix/HOTFIXVERSION
  7. Tag the hotfix version and push
    • git tag HOTFIXVERSION
    • git push origin HOTFIXVERSION

Squashing multiple commits into one

In your daily tasks if you need to squash multiple commits into one with a custom message just perform the following commands:

git reset --soft HEAD~n # where n is the number of last commits you want to squash into one

git commit --amend

git push -f

another (interactive) way is to use rebase like below:

git rebase -i HEAD~n

the last n commit messages will appear in your text editor. You will see something similar to:

pick 0123456 Message 1
pick 1234567 Message 2
pick 2345678 Message 3

you could just pick the first commit and squash the rest like that:

p 0123456 Message 1
s 1234567 Message 2
s 2345678 Message 3