Improve error handling when wrong branch is pushed

Fixes 1051