Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open 2 Visual Studio instances, with same Git projects and different branches

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?

like image 479
Ewerton Avatar asked Apr 17 '16 19:04

Ewerton


People also ask

Can I have two git branches open at once?

You can have many branches in your repository, but only one of these will be "checked out" as the working-tree so that you can work on it and make changes. git worktree adds the concept of additional working trees. This means you can have two (or more) branches checked-out at once.

How do I compare two branches in git using Visual Studio?

To compare your currently checked out branch with other branches using Visual Studio, you can utilize the branch picker hosted in the status bar and the Git changes tool window to choose any local or remote branch to compare with. Right click the branch you are targeting and select Compare with Current Branch.

How do I pull a different branch in Visual Studio?

Open the Team Explorer and open the Sync view. Then select the Pull link under Incoming Commits to pull remote changes and merge them into your local branch. Pulling updates files in your open project, so make sure to commit your changes before pulling.

How do I open the same project twice in Intellij with another branch?

The best you can do is to clone the repository twice on your local machine into two different directories, and then simply open up the appropriate directory in each IDE instance.


4 Answers

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.

like image 188
Ripp_ Avatar answered Oct 27 '22 08:10

Ripp_


If you need to open the code in Visul 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 to checkout 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 any external.

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 woorktree is now working and supported by GUI based tooling, this is probably the solution to use today.

like image 24
milbrandt Avatar answered Oct 27 '22 07:10

milbrandt


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

like image 2
Stijn De Cat Avatar answered Oct 27 '22 07:10

Stijn De Cat


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

like image 1
Franco Petra Avatar answered Oct 27 '22 08:10

Franco Petra