How best to squash old commits

timpone picture timpone · Jan 12, 2016 · Viewed 13.1k times · Source

A developer recently left who left a ton of commits in a repo from a few months ago that are just like 'updated'. Ideally, I'd like to squash them into a single commit but I have only done this for recently commits.

How would I do it for like the following commits (assuming the from 2 months ago means that there are hundreds of these)?

.... from 2 months ago

aabbcc updated
aabbdd updated
aabbee updated
aabbff updated

Not wanting / needing anything fancy, just a simple solution. These commits haven't been publicly shared (other than with me today) so no issue of upsetting other people's commit history.

Answer

CodeWizard picture CodeWizard · Jan 12, 2016

In order to do a git squash follow those steps:

// X is the number of commits you wish to squash
git rebase -i HEAD~X

Once you squash your commits - choose the s for squash = it will combine all the commits into a single commit.

enter image description here


You also have the --root flag in case you need it

try: git rebase -i --root

--root

Rebase all commits reachable from <branch>, instead of limiting them with
an <upstream>.

This allows you to rebase the root commit(s) on a branch.  
When used with --onto, it will skip changes already contained in `<newbase>`   
(instead of `<upstream>`) whereas without --onto it will operate on every 
change. When used together with both --onto and --preserve-merges, all root 
commits will be rewritten to have `<newbase>` as parent instead.`