Disable commit if not on a branch

I want to disable committing if you are not on a branch.

I have lost some code on a development server, because code was committed, but never pushed...

Is it possible to stop git from committing, if you are not on a branch?

Update: I am not on a branch since I have done "git checkout mytag" before.

Answers


You can use Git Hooks to achieve that.

I've found a pre-commit hook on GitHub that does exactly what you want:

#!/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

Put it in a file named pre-commit in .git/hooks directory and make this file executable.


In principal, if you use a git repository for your project you are always on a branch, so the first part of your question is confusing. Then, for the second statement, it should not be possible to lose code with git, you can retrace your commits to find the code back. But the confusing part of this part is that you make commits locally and push them to your remote branch. Then, lastly, to answer your question, depending on your environment/IDE, it is possible to stop committing by simply not committing. Since this is mostly a manual task.

I would have like to comment this (not enough points), since it is wise that you explain your situation a bit better and mention what IDE you're using and the branch and possible commits/commands you made.

About losing the code, you can revert your branch to a previous commit. If you stash your current changes you even not necessarily lose any of the code since that commit. Check the following thread:

How to revert Git repository to a previous commit?


Need Your Help

java Holder vs document-style, or why use Holders

java web-services java-ee jax-ws cxf

I've started to use document-style web-services recently.

Increment/alter integer column of several records at once in PostgreSQL

sql postgresql postgresql-9.1

I have records who have a priority, now I want to give the user the ability to select some records and add +200 priority to all selected records.