3

I want to match some code lines between the main branch and the test branch, but have each of the branches in their own VS code window. Is it possible to maintain the git changes in the main branch with its own VS Code window when performing git switch test on different VS 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 instance of VS 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 VS Code window.

starball
  • 20,030
  • 7
  • 43
  • 238
  • and then you change a file and to what branch should it go, at ALL times there is only one branch/commit pointed to by HEAD – rioV8 Feb 11 '23 at 02:56
  • 1
    look into cherry pick merge or interactive merge – rioV8 Feb 11 '23 at 02:57
  • Short answer, no not unless you cloned two separate git repositories just so you could have each one on a separate branch ...but that would be crazy :) – topsail Feb 11 '23 at 02:58
  • I'm just wondering if that's possible. Now, thanks for the enlightenment. – Ginoong. Flores Feb 11 '23 at 03:05
  • 1
    you can't do because git switch change your file structure based on your branch HEAD , then any instance of VScode will display the changed source file You can try git diff main..test to view the difference – Vignesh Feb 11 '23 at 03:07
  • 3
    @topsail it's not crazy. git has a built-in feature expressly for that without "craziness". see my answer. – starball Feb 11 '23 at 05:35

1 Answers1

6

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.

starball
  • 20,030
  • 7
  • 43
  • 238
  • I Appreciate that suggestion. I'll check this out and marked this solved once I tried it. – Ginoong. Flores Feb 11 '23 at 05:26
  • 1
    I tried using git worktree and it worked. I, managed to have different git changes on the same project in two instance of VS Code window. Also, able to merge the branch from another worktree to the main branch by deleting that worktree. Although, I still need to tinker with different worktree commands to grasp it well. All in all this solves my problem at ease. Marked as solved @user – Ginoong. Flores Feb 11 '23 at 10:11