Multiple Git Branches in Multiple Eclipse Projects

2019-04-04 07:00发布

问题:

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 )?

回答1:

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.



回答2:

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...)



回答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.