Cleaning Up Git History with Integrate Rebase

I used to treat my Git history like a dumping ground having tons of tiny commits with minimal changes. It worked, but it made the history messy and harder to review. One tool that helped me clean it up is 𝗶𝗻𝘁𝗲𝗿𝗮𝗰𝘁𝗶𝘃𝗲 𝗿𝗲𝗯𝗮𝘀𝗲. Say I have 5 commits C1 to C5 and I want 2 "big" commits: • 𝗖_𝗮: A clean summary of C1, C2, C3 • 𝗖_𝗯: A clean summary of C4, C5 I need to run: $ 𝚐𝚒𝚝 𝚛𝚎𝚋𝚊𝚜𝚎 -𝚒 𝙷𝙴𝙰𝙳~𝟻 The editor opens with: 𝘱𝘪𝘤𝘬 𝘊1 𝘱𝘳𝘦𝘷𝘪𝘰𝘶𝘴 𝘤𝘰𝘮𝘮𝘪𝘵 𝘮𝘦𝘴𝘴𝘢𝘨𝘦 𝘧𝘰𝘳 𝘊1 𝘱𝘪𝘤𝘬 𝘊2 𝘱𝘳𝘦𝘷𝘪𝘰𝘶𝘴 𝘤𝘰𝘮𝘮𝘪𝘵 𝘮𝘦𝘴𝘴𝘢𝘨𝘦 𝘧𝘰𝘳 𝘊2 𝘱𝘪𝘤𝘬 𝘊3 𝘱𝘳𝘦𝘷𝘪𝘰𝘶𝘴 𝘤𝘰𝘮𝘮𝘪𝘵 𝘮𝘦𝘴𝘴𝘢𝘨𝘦 𝘧𝘰𝘳 𝘊3 𝘱𝘪𝘤𝘬 𝘊4 𝘱𝘳𝘦𝘷𝘪𝘰𝘶𝘴 𝘤𝘰𝘮𝘮𝘪𝘵 𝘮𝘦𝘴𝘴𝘢𝘨𝘦 𝘧𝘰𝘳 𝘊4 𝘱𝘪𝘤𝘬 𝘊5 𝘱𝘳𝘦𝘷𝘪𝘰𝘶𝘴 𝘤𝘰𝘮𝘮𝘪𝘵 𝘮𝘦𝘴𝘴𝘢𝘨𝘦 𝘧𝘰𝘳 𝘊5 I need to change it to: 𝘱𝘪𝘤𝘬 𝘊1 𝘱𝘳𝘦𝘷𝘪𝘰𝘶𝘴 𝘤𝘰𝘮𝘮𝘪𝘵 𝘮𝘦𝘴𝘴𝘢𝘨𝘦 𝘧𝘰𝘳 𝘊1 𝘴𝘲𝘶𝘢𝘴𝘩 𝘊2 𝘱𝘳𝘦𝘷𝘪𝘰𝘶𝘴 𝘤𝘰𝘮𝘮𝘪𝘵 𝘮𝘦𝘴𝘴𝘢𝘨𝘦 𝘧𝘰𝘳 𝘊2 𝘴𝘲𝘶𝘢𝘴𝘩 𝘊3 𝘱𝘳𝘦𝘷𝘪𝘰𝘶𝘴 𝘤𝘰𝘮𝘮𝘪𝘵 𝘮𝘦𝘴𝘴𝘢𝘨𝘦 𝘧𝘰𝘳 𝘊3 𝘱𝘪𝘤𝘬 𝘊4 𝘱𝘳𝘦𝘷𝘪𝘰𝘶𝘴 𝘤𝘰𝘮𝘮𝘪𝘵 𝘮𝘦𝘴𝘴𝘢𝘨𝘦 𝘧𝘰𝘳 𝘊4 𝘴𝘲𝘶𝘢𝘴𝘩 𝘊5 𝘱𝘳𝘦𝘷𝘪𝘰𝘶𝘴 𝘤𝘰𝘮𝘮𝘪𝘵 𝘮𝘦𝘴𝘴𝘢𝘨𝘦 𝘧𝘰𝘳 𝘊5 • The "pick" commits act as the base for each group. • The "squash" commits are merged into the one above. • The order does matter as squashed commits are merged into the "pick" commit above them, but Git will keep the latest changes from the squashed commits. Git will then prompt you twice to write new commit messages for 𝗖_𝗮 and 𝗖_𝗯. You can edit these messages to make them clear, concise, and more descriptive of the grouped changes. Instead of just one pass, alternatively, you can also do it in two passes: First, squash C1, C2, C3 into 𝗖_𝗮. Then, squash C4, C5 into 𝗖_𝗯. After rewriting history, commits are force-pushed to remote (GitHub, etc.) with: $ 𝚐𝚒𝚝 𝚙𝚞𝚜𝚑 --𝚏𝚘𝚛𝚌𝚎 The result: • Cleaner commit history • Easier code reviews • Better storytelling of your changes I still commit a bit messily sometimes 😅, but now I know how to clean it up! #𝗚𝗶𝘁 #𝗚𝗶𝘁𝗛𝘂𝗯 #𝗣𝗿𝗼𝗴𝗿𝗮𝗺𝗺𝗶𝗻𝗴 #𝗗𝗲𝘃𝗧𝗼𝗼𝗹𝘀

To view or add a comment, sign in

Explore content categories