4

Our team is about to make the switch from SVN to Git. We all use IntelliJ Idea as an IDE.

Seeing as many of the folks currently work on the SVN trunk on a daily basis, I am afraid that, despite training and many warnings, many folks will sometimes forget to branch and en up making edits in the master branch (I know I have don it !).

I realize that there are ways to protect master, or even avoid disallow direct commits, but is there a way to configure IntelliJ to somehow warn users, or otherwise disallow them from making edits while in the master branch?

1 Answers1

2

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.

Community
  • 1
  • 1
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • This `pre-commit` hook will only work on terminal right? – Deepesh Jan 11 '17 at 09:13
  • 1
    @Deep Yes, this is my understanding. Hence, you would still need to configure Bitbucket (GitHub, etc.) to block direct commits, because someone could willingly/accidentally disable the hook. – Tim Biegeleisen Jan 11 '17 at 09:14
  • Yes that can be done on `remote` I think there is no option to do it on the editor we use. – Deepesh Jan 11 '17 at 09:23
  • 1
    Accidentally committing to the wrong branch is a common thing. It won't only happen on `master`, it could happen on some random branch which didn't even exist an hour ago. My answer at least gives a strategy for coping with this situation. – Tim Biegeleisen Jan 11 '17 at 09:25
  • 1
    When you mention using pre-commit hooks, you might want to add a caveat that they can be bypassed with "--no-verify". – PatrickSteele Jan 11 '17 at 14:14
  • 1
    @PatrickSteele I couldn't agree more...this is why I think a repo side check is what should be relied on to enforce no direct commits to a given branch. – Tim Biegeleisen Jan 11 '17 at 14:16