【发布时间】:2015-02-24 19:47:04
【问题描述】:
这与在 git push.default matching 设置有关。此外,在此特定情况下分叉回购也不适用。我们在工作中使用 github,由于它不允许使用 pre-receive 钩子,我们必须尝试通过 pre-push 钩子来缓解这种情况(如果你问我,这有点半途而废,但确实如此)。
无论如何,我正在尝试编写一个pre-push 挂钩,以防止有人意外强制更新我们的master 分支(或我们想要的任何分支)。然而,当我尝试做一个简单的尝试获取分支名称的测试时,我似乎只能得到当前签出的分支。我已经使用了这三个 git“方法”来做到这一点:
$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')$(git symbolic-ref HEAD)$(git rev-parse --abbrev-ref HEAD)
这三个都给了我相同的结果。这是一个例子:
[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
现在应用强制更新:
[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)
如您所见,仅引用了 test3 分支。我想知道 git 什么时候会推送 master 分支。有谁知道这里的强制更新在幕后做了什么?
【问题讨论】:
-
我没有给你答案,但在环顾四周时,我发现了这个:gist.github.com/pixelhandler/5718585 似乎你可以使用它?
-
@loganfsmyth - 我认为这不能解决脚本使用
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')获取当前分支的问题,根据我上面的示例,该分支不起作用。 -
还有@loganfsmyth 这个命令
push_command=$(ps -ocommand= -p $PPID)只得到推送命令(git push -f是我的输出) -
您在使用 GitHub Enterprise 吗?如果是这样,他们似乎可以选择禁用强制推送(请参阅Blocking force pushes to a repository)。