I want to simply squash a bunch of git commits that are no longer needed. I attempted to follow documentation from this site, but I ended up with git saying there is nothing to squash. I only have one branch, so I followed that section. Any ideas?


If I were doing it I'd use "rebase". Assuming you have your branch checked out, and that it is branched from master, do

git rebase -i master

This will allow you to modify the commits from master to your current branch. In your case, replace pick with s (for squash) or f (for fixup, which is a squash that drops the commit message of the squashed commit). Save and exit (ZZ in Vim), and rebase will do its magic. Rebase is a more general solution as in the same operation you can remove, re-order, and reword commits.

The rebase settings will open in Vim, or whichever editor you set in the EDITOR environment variable. In Vim, navigate to start of the line with the commit you wish to squash (with arrow keys or mouse), cw to delete pick and enter insert mode, f for fixup or s for squash, then ZZ to save and exit.

As a side note, the link you gave suggests flatting your feature branch into a single commit. In my opinion this is an awful idea. Commits should represent a meaningful set of changes. A branch is the set of these changes that represents a feature. Squashing everything into one huge commit means it will be harder for other developers to follow your changes.

| improve this answer | |
  • Ok, I will give it a try. I am actually not using it for software development at all, but to track configuration files and other stuff in Linux. I want a backup that only goes back so far, so in essence, want to remove history greater than 30 days. That is why I thought squashing it would be ideal as well as remove unnecessary space taken up by the revisions. – Walter Apr 21 '11 at 12:15
  • Ok, I think it worked this time, when I did -i, I was prompted to edit the commit messages and the rebase actually appeared to take place. When I didn't do the -i earlier, none of the commits were actually squashed. – Walter Apr 21 '11 at 13:50
  • @Walter if you do need to compress git at all, I think git gc does that, which git does of its own accord anyway. – tobylane Apr 21 '11 at 16:15
  • @Walter, squashing commits doesn't take up less space (or the space saved is negligible). Git may not be the right tool for what you want to do. – Mark E. Haase Apr 29 '11 at 2:49
  • Yeah, it isn't designed for that, but I like it better than other available tools. – Walter May 3 '11 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.