简体   繁体   English

如何防止在分离的HEAD中提交

[英]How to prevent commit in detached HEAD

Why does git allow you to commit to a detached head? 为什么git允许你承诺一个独立的头? Is there any pre commit hook that can disable it? 有没有可以禁用它的预提交钩子? What is the purpose? 什么目的? Many new developers do this and I'd like to find a way to disable it. 许多新开发人员这样做,我想找到一种方法来禁用它。

This can be only prevented by a local git pre-commit hook, so developers would need to create it. 这只能通过本地git pre-commit钩子来阻止,因此开发人员需要创建它。 Add the your-local-project/.git/hooks/pre-commit file with the following contents: 使用以下内容添加your-local-project/.git/hooks/pre-commit文件:

#!/bin/sh

if ! git symbolic-ref HEAD &> /dev/null; then
  echo "You are in a detached head state! Commit has been blocked. (Use --no-verify to bypass this check.)"
  exit 1
fi

Make sure it's executable. 确保它是可执行的。 Credits go to svachalek 积分转到svachalek

Why should git prevent commiting in detached HEAD? 为什么git会阻止在独立的HEAD中提交? Detached HEAD means only that there is no pointer to the repository state you are working on. 分离的HEAD 表示没有指向您正在处理的存储库状态的指针。 It assumes that you know what you are doing. 它假设你知道你在做什么。

I would rather investigate why many developers in your team enter this state? 我宁愿调查为什么团队中的许多开发人员都会进入这种状态? Maybe they apply some weird worklow? 也许他们应用了一些奇怪的工作流程?

git checkout $commit-sha1 can lead to a detached HEAD. git checkout $commit-sha1可以导致分离的HEAD。 So does git checkout FETCH_HEAD . git checkout FETCH_HEAD A detached HEAD could be considered as a branch without a name. 分离的HEAD可以被视为没有名称的分支。 If it does not confuse you, you could just ignore it. 如果它不会让你感到困惑,你可以忽略它。 As @fracz said, you could prevent it by pre-commit . 正如@fracz所说,你可以通过pre-commit来阻止它。 You could also make it a branch with a name with git checkout -b some_name . 你也可以使用git checkout -b some_name作为名称的分支。 A post-checkout hook may help you to detect the detached HEAD state and make it a branch. post-checkout钩子可以帮助您检测分离的HEAD状态并使其成为分支。

Git uses this internally for many operations. Git在内部使用它进行许多操作。 The detached HEAD mode simply gets you on the (one, single, special) anonymous branch, and the anonymous branch can be given a name later. 分离的HEAD模式只是让你进入(一个,一个,特殊的)匿名分支,并且以后可以为匿名分支命名。

This is, for instance, how git rebase manages to copy the commits from their original chain to a new chain. 例如,这就是git rebase如何设法将提交从其原始链复制到新链。 First it checks out the --onto target commit ( --onto defaults to the <upstream> ) using this detached HEAD mode. 首先,它使用这个分离的HEAD模式--onto目标提交( --onto默认为<upstream> )。 Then, for each commit that is to be copied, it copies that commit (with git cherry-pick or something equivalent: the details vary depending on interactive vs non-interactive rebase, and if interactive, many more details). 然后,对于要复制的每个提交,它复制提交(使用git cherry-pick或等效的东西:细节因交互式和非交互式rebase而异,如果是交互式的,还有更多细节)。 Last, it moves the existing branch label so that it points to the final copied commit. 最后,它移动现有的分支标签,使其指向最终复制的提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM