1

During a git push, I know it's possible to cancel the push using the pre-push hook and exiting the script with a non-zero exit code.

Using a pre-push hook, I would like to intercept the current push, conditionally rebase, and then push the rebased tree (or the original if no rebase was needed). Is that possible?

cmoel
  • 148
  • 1
  • 8
  • [It's impossible to edit commits in the pre-push hook](https://stackoverflow.com/a/47078245/7976758). – phd Sep 01 '18 at 14:53
  • What's the condition under which you want to rebase? – Adam Sep 01 '18 at 19:33
  • @Horba I'd like to automatically rebase and autosquash any fixup commits before pushing. – cmoel Sep 01 '18 at 23:10
  • I think you will have to be satisfied with a wrapper script, call it `git ci` or something. It would have to iterate, rebasing and pushing until the push succeeds (the push might fail if somebody else has just pushed, so you need to iterate). You then also need to decide what your criteria are for the script to abort. Obviously a conflict would abort, but should perhaps _any_ upstream modification to a file you are pushing be cause for a human to re-examine the diffs before deciding to push again? Are there other failure conditions? All up to you. – Mort Sep 02 '18 at 02:58

1 Answers1

1

Git doesn't allow you to modify the contents of the push using the pre-push hook. At the point the hook is called, all the refs on both sides and their intended values are already resolved, and Git doesn't re-read that data after the hook.

Certainly it's possible (although inadvisable) to make changes to the repository in a pre-push hook, but whatever changes you make won't have any effect on the data that is pushed.

bk2204
  • 64,793
  • 6
  • 84
  • 100