0

After running git rebase -i master I always wish to write :2,50s/pick/squash/g in the vim window that opens. Is there a way to automate what will be written to vim?

This answer shows how to write vim commands from shell, but the problem is in my case the git commands open up vim, and it's not in my control.

6
  • The editor you use should be in your control. Here's how to change it if you wish. (That is for changing to Vim, but you can change it to your preferred editor.) Commented Jul 7, 2022 at 2:50
  • 2
    Side Note: if you always want to squash down into a single commit, you could consider git reset --soft to the parent of the first commit and then make one new commit. (The parent of the first commit will probably always be git merge-base master HEAD.) Commented Jul 7, 2022 at 2:55
  • @TTT is it possible to rebase instead of merge? Apologies if that is a dumb question. Commented Jul 7, 2022 at 7:37
  • My suggestion was neither merge or rebase, and instead use reset to effectively squash all of your commits into a single commit. The outcome would be the same as what you're doing with the interactive rebase and squashing all of your commits up into the first one, except with reset you have to write the new commit message yourself. (Compared to with interactive rebase where it will show you the 50 messages at the end and let you edit it, or you can use "f" instead of "s" to avoid that.) The merge-base command was simply to help you find the parent of the first commit to reset back to. Commented Jul 7, 2022 at 14:11
  • 1
    Besides @TTT's git reset --soft you can also create a new branch name and run git merge --squash to build the same commit you'd build with git reset --soft. That is, you'll create a new branch based on the merge base commit (perhaps using master directly to find it), git switch to the new branch, then git merge --squash development-branch. Commented Jul 7, 2022 at 14:28

2 Answers 2

4

Create file

~/.vim/after/ftplugin/gitrebase.vim

try 2;'} s/^pick/squash setlocal modified catch endtry 

Must have filetype plugin on to work.

Sign up to request clarification or add additional context in comments.

Comments

1

You can define a key mapping in ~/.vimrc like this:

nnoremap ,rb :2,$ s/^pick/squash/<cr> 

Then in the vim session started by git rebase, you just press ,rb if you want to squash all the commits.

Or define as a vim function:

function GitRebaseSquashAll() :2,$ s/^pick/squash/ endfunction 

and call it with :call GitRebaseSquashAll().

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.