0

I'd like git rebase to never continue if there are new/untracked files.

Sometimes, during an interactive rebase, I run:

exec git undo --soft

(git undo is from tj's git-extras)

However, sometimes the commit I want to edit, simply adds new files. In this situation, the exec git undo --soft will not pause rebase. My commit I want to edit, will have effectively disappeared, the rebase will finish, and then.. the rebase completes, and I have the untracked files just sitting in my working directory.

Is there a way to configure git rebase to not simply continue when there are untracked files?

Devin Rhode
  • 23,026
  • 8
  • 58
  • 72

1 Answers1

1

git rebase -i stops after any exec line which has a non-zero exit code. So you could check for untracked files in a separate exec line or by executing the check last in your exec line

pick deadbeaf some commit to pick
exec git undo ...
exec u="$(git ls-files --others --exclude-standard) && test -z "$u"
pick c0ffeeba or you could use the next line:
exec git undo ...; u="$(git ls-files --others --exclude-standard) && test -z "$u"

Of course the "is something untracked" check can be put into a shell script or a custom git alias, so the exec line becomes shorter, e.g.:

git config --global alias.hasuntracked '!u="$(git ls-files --others --exclude-standard)" && test -z "$u"'

and then in your rebase todo list just:

pick beefb015 another commit
exec git undo ...; git hasuntracked
knittl
  • 246,190
  • 53
  • 318
  • 364
  • I'm glad you mentioned this right away: `stops after any _exec_ line which has a non-zero exit code`. Could I also just directly `exec git undo --soft && exit 1;`? – Devin Rhode Nov 07 '22 at 02:20
  • Ok, turns out that statically doing `exit 1` doesn't work well. When you are all done, commit, run `git rebase --continue`.. you will then be thrown BACK into editing the commit AGAIN, to break the loop, you need to `git rebase --edit-todo` and then delete the `exec git undo --soft && exit 1` line.. then `git rebase --continue`.. and you'll actually continue.. – Devin Rhode Nov 07 '22 at 04:42
  • @DevinRhode yes, the `exec` line will be retried. If you have for example `exec make test`, you don't want to continue if your tests still fail :) – knittl Nov 07 '22 at 18:53
  • yeah, it's almost like git rebase should create a new `test` command, a slightly different version of `exec`. `test` gets rescheduled automatically, while `exec` only runs once. – Devin Rhode Nov 09 '22 at 20:30
  • While we are at it, git should also add a `redo` command too, which is kinda like `edit` but pops your changes into working directory. – Devin Rhode Nov 09 '22 at 20:31