Shoe
Shoe

Reputation: 76298

Is there a way to make a local branch immutable?

I'm extremely stupid and sometimes I merge a "feature branch" in the wrong master branch as opposed to the develop branch and that of course leads to pain and suffering.

Since it's the second time already that this has been happening, I wonder if there's a way for me to make a branch immutable locally.

I've already tried with this pre-commit hook to prevent me from directly making commits on said branch, but this is not stopping merges as in:

git checkout master
git merge wip

Is there a way to prevent all possible changes to a local branch? If not, is it at least possible to prevent changes from being pushed?

Upvotes: 5

Views: 884

Answers (1)

larsks
larsks

Reputation: 312770

You cannot make a local branch immutable (there is no local hook that runs before a merge operation). However, if you're not going to be making commits on the branch, just don't check it out. If you have it checked out already, delete the local branch:

$ git checkout some-other-branch
$ git branch -D master

Now when you attempt to checkout the master branch it will fail:

$ git checkout master
error: pathspec 'master' did not match any file(s) known to git.

If you ever need to refer to the upstream master branch (e.g., for a diff), you can just refer to it directly:

$ git diff origin/master

Upvotes: 2

Related Questions