Git History Squash for fun and profit

Suppose you are developing a boring Jenkins pipline (like Jenkinsfile-s) with no time at all.

You are forced to commit and then run the jenkins pipeline. Jenkins download the code from your LOCAL repository.

To avoid commit& push roundtrip you are using the simple git daemon command to expose your local repository to jenkins.
So jenkins see every commit you has just done.

And you end up doing a lot of commits, full of trial and errors: you would like to “squash” them before pushing your work to your remote repository, to avoid co-worker laugh your wasted time(!)

Git can do that, but I have an hard time to find the easier way of doing it, even reading git books!

So let me explain to you:
First of all, take the hash of your first commit, i.e. badda1319d17ce3fd454eae9ad12656151051e

Then, during development commit using this boring line

git commit -a --squash=badda1319d17ce3fd454eae9ad12656151051e -m ""

After your work is ended and you are ready, suppose you have 20 commit. Suqash them with:
git rebase --autosquash --interactive HEAD~20
Rebase will have already auto-squashed the commit for you, so take a look and then you are free to exit from the editor.

 

As usual, avoid rebasing code already pushed: because of the git ditributed nature, git will stop you because there is the risk of rewriting co-worker history!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.