Is there a way to make a local branch immutable?

萝らか妹 提交于 2019-12-07 11:02:32

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/36102161/is-there-a-way-to-make-a-local-branch-immutable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!