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