atconway
atconway

Reputation: 21314

How to make separate working directories for separate branches using TFS Git?

I have (2) separate branches in a TFS managed Git repository that I'm working with in VS.NET 2013 that are quite a bit different (i.e. solution and project structure are different during major upgrade process). I notice that if I have Windows Explorer opened and switch between the repositories, the working directory is automatically updated and restructured with the new files.

This is nice because seamlessly Git will bring into the working directory all of the current files for the active branch I selected. However what I really need is for separate working directories so I can see all of the project files for both branches at the same time. This as opposed to using the same directory that's being essentially overwritten each time I change branches.

I have looked at every 'right-click' or 'settings' option available in the IDE and can't seem to find a way to assign separate directories for each branch. How can I assign different working directories to separate Git branches using VS.NET 2013?

Upvotes: 2

Views: 2285

Answers (3)

Fabio Marreco
Fabio Marreco

Reputation: 2303

Starting from git version 2.6.0 you can use the git worktree command to create a separate worktree to have work in parallel.

git checkout branch1
// working on branch1
git worktreee add ../branch2-workspace branch2
cd ../branch2-workspace
//working on branch2

when you are done with the folder, just delete it, and run git worktree prune

Upvotes: 1

Joseph K. Strauss
Joseph K. Strauss

Reputation: 4913

As long as you are not planning on committing, you can check out a commit to a different directory. (You could commit also, but that will make things really confusing, so do not do that.)

git checkout <branch-1>
cd <path/to/other/dir>
git --git-dir=<path/to/repository/.git>  checkout <branch-1>
cd <path/to/repository>
git c
git checkout <branch-2>

The checkout command does change the branch in the actual repository, so it must be executed before checking out the repository branch.

EDIT: Better Alternative

 git checkout <branch-1>
 git --work-tree=../test2 co HEAD
 git checkout <branch-2>

This is better for because

  • there are much fewer steps
  • it is safer because you are not checking out a different commit into the the other directory

There may still be issues when checking out a second time to the alternate directory. You can just delete it and start over since you are using it as view-only.

Upvotes: 1

Edward Thomson
Edward Thomson

Reputation: 78743

You cannot. Unlike TFVC (where branches exist inside the path space of the repository), in Git, branches exist at the repository level.

You should clone the repository a second time, to a second location, and you can keep each local repository checked out to a different branch.

Upvotes: 6

Related Questions