git - Visual Studio Code/Git : Is there any way to prevent editing on master branch, 还允许 merge 吗?

标签 git visual-studio-code

在处理项目时,我想总是在 master 以外的分支上进行编辑。 Master 用于工作的、经过测试的代码。

我有时会忘记这一点,并开始在 master 分支上编码。然后我必须重置更改等。

我想知道有什么方法可以使主文件只读,但仍然允许 merge 到其中。

最佳答案

您可以添加一个预提交钩子(Hook)( like this gist ,它可以阻止对 master 的意外提交)
你可以适应 hook to bypass it for merge commits .

VSCode 应该通过 Git 尊重预提交,如果您在 master 上,您将无法立即提交。 .
您需要存储,切换分支,存储流行音乐。

实际上,与new git switch command (Git 2.23,2019 年 8 月),你甚至不必藏起来。

example section of git switch 处理您的场景(开始在“错误”分支上工作)

After working in the wrong branch, switching to the correct branch would be done using:


$ git switch mytopic

However, your "wrong" branch and correct "mytopic" branch may differ in files that you have modified locally, in which case the above switch would fail like this:


$ git switch mytopic
error: You have local changes to 'frotz'; not switching branches.

You can give the -m flag to the command, which would try a three-way merge:


$ git switch -m mytopic
Auto-merging frotz

关于git - Visual Studio Code/Git : Is there any way to prevent editing on master branch, 还允许 merge 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57566495/

相关文章:

git - 管道 git diff 到 git apply

java - 不使用 GIT 将 WAR 文件部署到 Openshift?

git - 为什么 git cherry-pick 会比 git rebase 产生更少的冲突?

typescript - vscode-test - 调用 runTests 时出错

visual-studio-code - 每次我从 WinSCP 打开文件时,VS Code 都会打开 "VS"和 "code"窗口

c - 如何初始化结构值并正确输出结构

git clean 不清理 stash 文件

git - 使用 Maven/Jenkins/Git 创建发布标签

javascript - fs.readFileSync 有时仅在特定情况下返回有效字符串

regex - 为什么我的正则表达式与 Visual Studio 代码中的空行/回车符匹配?