150

I need to open 2 Visual Studio instances: one will be to just look at the code of the Project X / Branch 1, and the other will be used to code in the Project X / Branch 2.

How can I do that without losing changes when committing?

Appulus
  • 18,630
  • 11
  • 38
  • 46
Ewerton
  • 4,046
  • 4
  • 30
  • 56
  • 36
    clone the repo twice to different directories – Ewan Apr 17 '16 at 20:36
  • 1
    Huh? That's the *whole point* of using version control in the first place, isn't it? Why would you even lose changes? – Jörg W Mittag Apr 18 '16 at 01:27
  • 2
    Do you know that you can diff branches directly? `git difftool branch1..branch2 -- /path/to/file.c` will do that. Also possible: if you checked out `branch1`, you could directly `git difftool branch2` to diff all files between 1 and 2. Or `git diff --name-only branch2` lists you all files that are different between `branch1` and `branch2`... – eckes Apr 18 '16 at 09:48
  • https://stackoverflow.com/questions/2048470/git-working-on-two-branches-simultaneously could answer this question perhaps – Maryam Jul 17 '17 at 01:07
  • @Maryam apparently, VS still doesn't support git worktrees, which is the suggested solution there :^/ . – ThePartyTurtle Jul 28 '17 at 15:20

6 Answers6

195

The issue here isn't to do with Visual studio, but how Git works. When you check out a branch in git, it places that branch into your working tree or (file structure, whatever you wish to call it).

With git you can only have one branch checked out at a time, but wait there is a solution! By utilising the git worktree command, you can create a second worktree for the same repository in a different directory. You can then open that work tree in visual studio to have two different branches checked out.

Let's say you have C:\projects\the_project and you want to make a new work tree at e.g, C:\projects\the_project_2, open git bash, navigate to the project's directory and run

git worktree add ../the_project_2 <branch>

where is the branch you want checked out in the new work tree.

This will create a new directory "C:\projects\the_project_2") and checkout the branch into it, without having to re-clone the repository.

For more information see the git worktree documentation.

Note: Earlier versions of Visual Studio does not know how to treat additional work trees, and will not recognise them as git repositories.

Siraj Alam
  • 9,217
  • 9
  • 53
  • 65
Ripp_
  • 2,298
  • 1
  • 9
  • 12
  • 5
    To clarify: `C:\projects\the_project` is the Git repository containing the solution(s), projects, sources... Afterwards open in one VS the solution from `the_project` and in a 2nd VS the solution from `the_project_2`. Then you can change the branch in each VS instance individually. – huha Nov 29 '19 at 08:58
  • Great tutorial by GitKraken: https://www.gitkraken.com/learn/git/git-worktree – glimmbo May 26 '23 at 17:11
6

If you need to open the code in Visual Studio, it is necessary to checkout the branch. As you can't checkout two different branches in the same directory at the same time, you can't avoid but checking out each branch in a separate directory.

But you can set the remote of one directory to the other git directory, so that you can synchronize locally and don't need to have an external link.

Assume you want to have both branches as subdirectories of a root common directory ProjectX:

cd ProjectX
git clone -b branch1 <remote repo of project X> directory_branch1 
git clone -b branch2 directory_branch1 directory_branch2

Update May 2019: As git worktree now works and is supported by GUI based tooling, which is probably the recommended solution to use today.

Hari Reddy
  • 339
  • 3
  • 15
milbrandt
  • 1,438
  • 2
  • 15
  • 20
5

"clone the repo twice to different directories" is the quickest easiest workflow as @ewan said in the comments.

Franco Petra
  • 688
  • 7
  • 18
3

If you don't care for the git connection of the source branch (the one you just look at but not modify), you can always make a copy of the folder containing the solution when said branch is active. then you can open a non-source controlled version of that branch once the target branch is active

-1

Let say your project is my_project then now clone same repository of my_project from git in different folder location and open it in new window and then switch to desired branch.

user3930098
  • 395
  • 7
  • 14
-2

I just figured out that its possible and here is the way we can do it -> go to command palette (shift + cmd + P) on mac and type dupl it will show you option - Duplicate workspace in new window so click on that screenshot from vscode

Irfan Pathan
  • 309
  • 1
  • 3
  • 14
  • This creates two views of the project, but it doesn't allow you to view two different branches: if you checkout a branch in one of the views, the other view will also switch to that branch. – Aaron Dunigan AtLee Feb 09 '23 at 22:26