Sunday, May 24, 2015

Git with Eclipse (2) - creating local repository with EGit in Eclipse

This post describes how to create a local repository using EGit. For how to configure EGit in Eclipse, please read Git with Eclipse (1) - configuration of EGit in Eclipse. All screenshots below are from Eclipse Kepler.

Create Local Repositories

Compare to centralised version control system SVN or CVS, you can easily create local repositories in Git. It enables you to version your work locally before sharing with other people.

Firstly, you need create a project in the workspace. Then right click on the project and select Team-> Share Project...


Select Git as the repository type and click Next.
























In the next window, select your project and click Create button to create a Git repository. You can also tick 'Use or create repository in parent folder of project' to create the repository in the parent folder of the project. But it is not recommended and EGit will not allow if the project is under Eclipse workspace folder like my example in this post. The reason will be discussed later. 

In the popup dialog, specify a directory in Parent  directory. It can be any directory on your disk. But it is a good practice to use the parent directory of Eclipse workspace, because EGit will move the project folder from workspace folder to the repository folder. A new developer might be confused why he could not find the project folder in workspace. By doing this, it is easier to know where is the project folder on the disk. Then give a name of the repository. Click Finish to return to the Configure Git Repository dialog. EGit tells you the projects will be moved from the Current Location to the Target Location in the lower table of the dialog.
























The Path within repository enables you to specify a sub folder inside the repository folder, and the projects will be moved to the sub folder you specified. I leave it blank so the project will be in the repository folder. Then click Finish button to create the repository.

Once the repository is created, you will notice a ">" sign appears next to the question mark icon on the project folder and some files are also have a question mark icon on them. The question mark icon means the project folder and all those files are new in the repository. And the ">" sign tells you there is modification inside project folder. You may also notice there is no question mark on the bin folder. It is because the folder has been added in to .gitignore file. All files and folders specified in .gitignore file will be ignored by Git and will not be shared into the repository. The files to be ignored are typically class files and sometimes the project settings files.






















Now you are ready to commit your project to the local Git repository. For some basic use cases of EGit, please read Git with Eclipse (3) - basic use cases in EGit with local repository.

Eclipse workspace and repository working directory

Unlike SVN or CVS has to put the repository metadata folder inside the project folder, Git can put its metadata folder .git outside project folder. In my example, the .git folder is under GitRepository folder. I can share more than one projects in this repository and they all use the same .git folder to store the repository metadata.

According to EGit user guide, “it is not a good idea to make a project the root folder of your repository”. Because it will not possible to share more than one project in that repository.

And there are reasons why EGit keeps the repository working directory outside Eclipse workspace.

  • When calculating the changes before committing, only active projects folders will be scanned. Irrelevant folders like .metadata or any inactive projects inside Eclipse workspace will be skipped.
  • If .git folder is inside workspace folder, it is unclear whether Eclipse will do unwanted folder scan on the .git folder.
  • Repository data is independent to the Eclipse workspace.

Reference:

No comments:

Post a Comment