14

We're in the process of converting from Subversion to Git and one thing that I'm struggling with in Git is the method of switching branches.

My understanding is that at both at the command line and using EGit in Eclipse that switching to a different branch replaces the contents of the Eclipse workspace folder with that of the desired branch. This implies only one branch can be open at any one time.

In SVN I could arrange my Eclipse workspace similar to:

Workspace/
   Project1Branch/
   Project2Branch/
   Project3/
   Project4Branch/

Is it possible to have multiple Git branches open at the same time in the same Eclipse/Egit workspace ( or any other environment for that matter )?

Greg Kennedy
  • 805
  • 1
  • 11
  • 23
  • You can have multiple branches available in Git at the same time, but only 1 branch can be checked-out at a time. What is supposed to happen when file `foo.c` is different in 2 different branches that are both open? – Mark Leighton Fisher Jan 28 '13 at 21:38
  • In SVN if foo.c is changed in both branches then there is a merge conflict that needs to be resolved before the branches can merge back to the trunk/master. I was looking for similar functionality in Git but it's looking more likely that I can't have both master and branch (or any other branch) open at the same time. – Greg Kennedy Jan 29 '13 at 16:57
  • 1
    You are right -- you can have only 1 branch open at a time. – Mark Leighton Fisher Jan 30 '13 at 12:24
  • Thanks for your help @MarkLeightonFisher. It looks like I'll just have to live with it so. – Greg Kennedy Jan 30 '13 at 17:05
  • @MarkLeightonFisher you used 3 different terminologies which is confusing. You say "You can have multiple branches **available**", "only 1 branch can be **checked-out** at a time", "1 branch **open** at a time". Basically your comments say nothing to a beginner. – haelix Sep 05 '18 at 09:39
  • @Mark Leighton Fisher: What should happen? An IDE could flag one branch as a work branch, disallow writes to the others and show a banner in editor windows if the file is from a read only branch. This way you could at least view the read only branches. – Hoov Oct 09 '20 at 12:21

3 Answers3

19

After much testing and a sanity check from @MarkLeightonFisher it seems it is not possible to have multiple Git branches open in the same Eclipse workspace at the same time.

Greg Kennedy
  • 805
  • 1
  • 11
  • 23
8

Indeed you can't import multiple branches from the same git repository into the workspace, because it only has one working directory which can contain only one checked branch. Here's a very reasonable workaround that accommodates this feature of git (I do it and it works well): Clone the git repository in a second directory (or 3rd or 4th even), then change the name of the project manually by editing .project and changing the <name> element. You can now import this project alongside the original, and work on multiple branches at the same time in the same workspace. Caveat: Get very friendly with the "Close Project" menu item so you can avoid cross-editing (it gets confusing sometimes when you have many source windows open with the same file name...)

A. Assaad
  • 81
  • 1
  • 1
3

Actually i managed to do it :

You can have multiple workspaces for one git repository : this is called "worktree". (limitation is that for obvious reason, you can't checkout same local branch in 2 different work trees. Well, you shouldn't, but some commands may lead you to be in such situation.)

https://git-scm.com/docs/git-worktree

Then you can create one eclipse-workspace for each of your work-tree.

Pellekrino
  • 346
  • 2
  • 14
  • 1
    the question is how to open the same project on multiple branche in the same workspace. Of course you can create worktree or another clone on another branch but Eclipse will not load the same project in the same workspace. You'll unfortunatly have to rename it in order to do so like explained above. – SeB.Fr Aug 14 '17 at 08:07
  • Yes as said, you need then to have one eclipse workspace per git workspace you created with git worktree. Then you'll have to work with several eclipse instances opened. This is not ideal, but afaik, as close as what was requested. At least better than a blank 'not possible' solution. – Pellekrino Aug 16 '17 at 10:06