Last Updated: December 28, 2017
·
15.36K
· craigmarvelley

Use Vim visual blocks to squash multiple git commits

When rebasing interactively with git, often we need to squash a sequence of commits in series, i.e. change a set of commits from this:

pick f7f3f6d changed my name a bit
pick 310154e updated README formatting and added blame
pick a5f4a0d added cat-file

to this:

pick f7f3f6d changed my name a bit
squash 310154e updated README formatting and added blame
squash a5f4a0d added cat-file

When you need to change a lot of commits from squash to pick it's a bit tiresome to change them one by one - so, if you're using Vim, it's a lot easier to instead:

  • place the cursor at the start of the line with the first commit you want to squash
  • enter visual block mode (CTRL-V)
  • select all the rows you wish squash
  • hit 'c' and type 'squash' to replace the 'pick' command
  • press ESC to apply that change to all the selected rows.

Your fingers will thank you for it!

1 Response
Add your response

Better yet, add this to .vimrc:

map s :2,$s/^pick/squash/<CR>

Now when you enter vim you just press 's' and it does the rest.

over 1 year ago ·