6

While working on projects, I want to always edit on branches other than master. Master is for working, tested code.

I sometimes forget this, and start coding on master branch. Then I have to reset changes, etc.

I wonder is there any way to make master files read-only, but still allow merges into it.

O. Altun
  • 685
  • 7
  • 20
  • 2
    Allowing merges into it by *definition* makes it not read-only. Though assuming you're doing pull requests, you can require a review before you allow a merge. The main this is to always check which branch you're actually working on before making a commit. – Obsidian Age Aug 20 '19 at 03:55
  • 2
    if you haven't commit the new changes just create a new branch and the modified files will be moved over to the new branch and you can commit – rioV8 Aug 20 '19 at 09:09

2 Answers2

4

Another option, with VSCode 1.68 (May 2022):

Git: Branch protection

With the new git.branchProtection setting, you can configure specific branches to be protected.

VS Code will avoid committing directly on protected branches and will offer you the chance to create a new branch to commit to instead.
You can fine tune this behavior with the git.branchProtectionPrompt setting.

That way, only new branches are created, before being merged back to main.


VSCode 1.70 (July 2022) fixes a regression reported in issue 153787 and illustrates how that settings work:

  • Launch the latest VS Code Insiders
  • Open a folder/workspace that contains a git repository
  • Ensure that the scm.showActionButton setting is enabled
  • Ensure that all three properties of the git.showActionButton setting are enabled
  • Use the git.branchProtection setting to enable branch protection for the current branch
  • Make a change to a file:
    • Confirm that the "Commit" action button is shown and it has the $(check) codicon
  • Set the git.branchProtectionPrompt setting to alwaysCommitToNewBranch
    • Confirm that the "Commit" action button is shown and it has the $(git-branch) codicon
Wolf
  • 9,679
  • 7
  • 62
  • 108
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
3

You could add a pre-commit hook (like this gist, which stop accidental commits to master)
You can adapt that hook to bypass it for merge commits.

VSCode should respect, through Git, that pre-commit, and you would not be able to commit right away if you are on master.
You would need to stash, switch branch, stash pop.

Actually, with the new git switch command (Git 2.23, August 2019), you don't even have to stash.

The example section of git switch deals with your scenario (starting working on the "wrong" branch)

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
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • This prevents committing on the master, but it does not prevent starting to change the files. – O. Altun Oct 05 '19 at 19:43
  • @user9144 no, but at least you can then switch branches (while keeping your index), and apply your current changes to the right branch. See the new [`git switch` command](https://stackoverflow.com/a/57066202/6309). – VonC Oct 05 '19 at 19:45
  • @user9144 I have edited the answer accordingly. The point is: start working from wherever, you can then switch and merge your work in progress to the right branch later on. – VonC Oct 05 '19 at 19:50