Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch to another Git branch in a Visual Studio Code window while maintaining the main branch changes on another instance of Visual Studio Code

I want to match some code lines between the main branch and the test branch, but I have each of the branches in their own Visual Studio Code window. Is it possible to maintain the Git changes in the main branch with its own Visual Studio Code window when performing git switch test on different Visual Studio Code window?

I tried to have two workspace, let's say workspace-main supposed for the main branch and workspace-test for the test branch. Therefore, I managed to have two instances of Visual Studio Code window with the same project. However, whenever, I performed the git switch test on the workspace-test separated window, the workspace-main window also changes its git branch to test.

I haven't seen yet any examples on the Internet or here. I'm wondering if that's possible rather than switching branches on a single Visual Studio Code window.

like image 450
ginoongflores Avatar asked Oct 30 '25 12:10

ginoongflores


1 Answers

Use git's worktree feature. (link to docs: https://git-scm.com/docs/git-worktree).

Worktrees allow you to have multiple checkout folders that can each be a checkout of a different branch / commit, but they share the same .git/ folder (Ex. fetch in on worktree fetches data to the shared .git/ folder, so other worktrees don't need to do the same fetch). It allows to view and edit multiple checkouts of different branches / commits at the same time, and have different working trees and staging trees for each.

Then just switch each worktree to the desired branch / commit and open them in different VS Code windows.

To create a new worktree, use add <path> [<commit-ish>]. See the docs for more info on command usage.

like image 56
starball Avatar answered Nov 02 '25 01:11

starball