Safe git rebase

Quite often, when I work on one, I find some ugliness in unrelated code, fix it and fix it separately. As a rule, there are several such commits, since I find similar problems later.

Finally, I reinstall (reorder and squash) so that commits make more sense. Sometimes I get merge conflicts, and sometimes the resulting processing tree is different from what it was before the reboot.

  • Could such a difference occur even when there were no conflicts?
  • Is there a way to get a (huge flashing) warning when a difference occurs?
+5
source share
1 answer

Could such a difference occur even when there were no conflicts?

Perhaps not, but human error is a factor that we must always consider.

Is there a way to get a (huge flashing) warning when a difference occurs?

This should do:

git diff ORIG_HEAD HEAD 

To do this automatically, use the post-rewrite hook (see Git Hooks ):

after rewriting

This hook is called by commands that rewrite commit (git commit --amend, git -rebase; currently git -filter-branch does not call it!). His first argument indicates the command to which he was called: currently one of the changes or rebase. In the future, additional arguments depending on the command may be passed.

Then in the hook script .git/hooks/post-rewrite we can compare ORIG_HEAD and HEAD and issue a warning if it is different:

 #!/bin/sh diff_rebase() { # echo "Old HEAD: $(git rev-parse ORIG_HEAD)" # echo "New HEAD: $(git rev-parse HEAD)" if ! git diff --quiet ORIG_HEAD HEAD; then >&2 echo "NOTE: diff detected after rebase!" else echo "rebase well done" fi } # bypass git amend [ "$1" = "rebase" ] && diff_rebase 

To make the hook accessible worldwide, consider the configuration of core.hooksPath , for example:

 git config --global core.hooksPath /usr/local/git-hooks/ 

Then save the script as /usr/local/git-hooks/post-rewrite , with the permission set set.

NOTE : unfortunately, it seems that the error related to the fact that interactive forwarding, namely git rebase -i , does not trigger the hook when using custom dir. (I am using git 2.9.2)

+3
source

All Articles