d6gifw
Last Updated: December 28, 2017
·
5.427K
· craigmarvelley
44c6fcfb46945625463e1847e1dca048

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!

Say Thanks
Respond

1 Response
Add your response

24167
None

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 ·
Filed Under