Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run git rebase --interactive in non-interactive manner?

Is it possible to do following?

  1. Make git rebase --interactive to just output standard boilerplate to a file, instead to outputting to a file and opening it in editor.
  2. Let the user edit the file.
  3. Let user re-run git rebase with the name of edited file.
  4. Go on with the usual rebase process.

Usecase: scripted rebasing of course. See how to re-order commits in Git non-interactively for example.

like image 752
pfalcon Avatar asked Sep 12 '12 18:09

pfalcon


People also ask

How do I turn off git interactive rebase?

you can abort the rebase by deleting the entire contents of the editor window and saving it, or causing the editor to close with an error code. In vim this can be accomplished with d SHIFT+g followed by :wq , or alternatively causing the editor to exit with an error as Mike H-R pointed out out using :cq .

How do I use git interactive rebase?

You can run rebase interactively by adding the -i option to git rebase . You must indicate how far back you want to rewrite commits by telling the command which commit to rebase onto. Remember again that this is a rebasing command — every commit in the range HEAD~3..


2 Answers

After some thinking and research, the answer turned out to be trivial: git rebase -i takes the editor name from the well-known EDITOR/VISUAL environment variables, so overriding that to point to a non-interactive script does the job.

However, EDITOR/VISUAL applies indifferently to the list of commits, commit messages when rewording and anything else. So, since http://git.kernel.org/?p=git/git.git;a=commit;h=821881d88d3012a64a52ece9a8c2571ca00c35cd , there's a special environment variable GIT_SEQUENCE_EDITOR which applies only to the commit list.

So, the recipe to re-order or flatten commits is:

Run: GIT_SEQUENCE_EDITOR=<script> git rebase -i <params>. Your <script> should accept a single argument: the path to the file containing the standard rebase commit list. It should rewrite it in-place and exit. Usual rebase processing happens after that.

like image 169
pfalcon Avatar answered Oct 06 '22 00:10

pfalcon


Adding on to @pfalcon's answer, you can use sed as your GIT_SEQUENCE_EDITOR. For example, I wanted to edit each commit, so I did this:

GIT_SEQUENCE_EDITOR="sed -i -re 's/^pick /e /'" git rebase -i 
like image 29
Leif Wickland Avatar answered Oct 05 '22 23:10

Leif Wickland