This has to do with using git's push.default
matching
setting from git < 2.0. Also, forking the repo in this particular instance is not applicable. We use github at work and since it does not allow a pre-receive
hook, we have to try and mitigate this via pre-push
hook (somewhat of a half-measure if you ask me, but it's something).
Anyway, I'm trying to write a pre-push
hook that will prevent someone from accidentally force updating our master
branch (or any branch we want). However, when I tried doing a simple test of trying to get the branch name, I can only seem to get the currently checked out branch. I've used these three git "methods" to do so:
$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
$(git symbolic-ref HEAD)
$(git rev-parse --abbrev-ref HEAD)
and all three give me the same result. Here is an example:
[dude (master)] $ git rebase -i head^
# reword a commit to get into a force-push state
[dude (master)] $ git checkout test3
[dude (test3)] $ git push
branch 2>: test3 # output by the hook
symbolic-ref: refs/heads/test3 # output by the hook
rev-parse: test3 # output by the hook
--- pre-push: nothing to check # output by the hook
To git@github.com:dude/stuff.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:dude/stuff.git'
hint: bla bla bla
now apply a force update:
[dude (test3)] $ git push -f
branch 2>: test3 # output by the hook
symbolic-ref: refs/heads/test3 # output by the hook
rev-parse: test3 # output by the hook
--- pre-push: nothing to check # output by the hook
Counting objects: 1, done.
Writing objects: 100% (1/1), 185 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To git@github.com:dude/stuff.git
+ 060fa0b...763516d master -> master (forced update)
As you can see, only test3
branch is being referenced. I want to know when git is about to push the master
branch instead. Does anyone know what a force-update does behind the scenes here?