The local branch called master
is not different than any other local branch as far as I know, at least from the point of view of Git and probably the Git plugin for IntelliJ. I think that protecting the master
branch from direct commit is absolutely something which needs to be on the repository side of things.
If you really want to block this locally, you can use a pre-commit hook.
That being said, if a developer accidentally begin working on master
locally and made a few commits, there is a fairly straightforward way to fix things:
git checkout master
# work, add files
git commit -m 'did some work'
git checkout -b feature_branch # create feature branch from local master
git checkout master # return to local master branch
git reset --hard HEAD~N # replace N with number commits made to master
The three immediate Git commands above create a new branch from master
which contains the same work which the developer already did. Then, we return to master
and remove the commits which the developer already made. Now the developer has the feature branch which he originally intended to have, and master
is restored to its initial state.