Monday, 17 March 2014

New Eclipse project connected to Git repository

You have a new Eclipse project to work on and you want to version control it on a Git repository on another machine. Here's how you do it.

1. On the server you wish to have your Git repository remotely stored, setup an empty repository. In our examples here the repository and project will be called UVaOnline

johan@htpc ~/gitrepos $ pwd
/home/johan/gitrepos
johan@htpc ~/gitrepos $ mkdir UVaOnline.git
johan@htpc ~/gitrepos $ cd UVaOnline.git/
johan@htpc ~/gitrepos/UVaOnline.git $ git --bare init --shared=all

2. Add a file to this empty project if you wish to make sure there is something to check out from Eclipse.

johan@htpc ~/gitrepos $ cd /tmp
johan@htpc /tmp $ git clone /home/johan/gitrepos/UVaOnline.git/
johan@htpc /tmp $ cd UVaOnline/
johan@htpc /tmp $ touch FOO
johan@htpc /tmp $ git add FOO
johan@htpc /tmp $ git commit
johan@htpc /tmp $ git push origin master


3. In Eclipse, add the Git Repository Exploring perspective via the menu Window -> Open Perspective.


4. Click the "Clone a Git repository and add the clone to this view" icon in the perspective.

5. In the first step of this wizard, enter the connection details for the Git repository. In my case I use my SSH user to log on the server.


6. If your project was emtpy, you only have the master branch to work with. Click next.


7. Choose where you wish to have your local Git repository on the machine Eclipse is installed on. I selected the "Import all existing projects after clone finishes".


8. Now a new repository should be available in the Git repository Explorer.


9. Try to fetch the repository if you want to see the refs configurred.


10. You see the connection URL again, next.


11. Here you could add or delete refs pointers. For our purposes, this looks alright.


12. To import the project into the Java/Java EE perspectives, click the "Import Projects" option in the menu.


13. Since this project is a bare bones project with no .project file with Java facets, we must select to import the project as a "Import as general project".


14. Probably the default name is good enough.


15. Now you could start adding file to your project. I made my project a Maven project here and added some source code. Once you're ready to commit to the remote repository, right-click and choose Team -> Synchronise Workspace.


16. In the next view, select the files you wish to commit and add them to Git index first.


17. Then right click again and commit.



18. If you do not click "Commit and push" the files are only added to your local git repository on the same machine. That can be good if you want to pile up a bunch of local commits before pushing to the remote repository. When you want to push all local commits, in the Java perspective right click and go via Remote to Push.


19. If it is the first time, you might need to add a spec for this. If you work on master which you do if you have followed the tutorial, simple add "refs/heads/master" as source and destination ref and click "Add spec".


20. New spec added.


21. Once finished, the commits you had locally are pushed to the remote repo.


Thats it. I have some more stuff on working with Git in Eclipse in this blogpost about Github cloning

For more advanced Git usage, you might need the console. That's no problem, simply make sure to refresh the Eclipse project after working in the console since Eclipse must refresh the .git folder etcetera. See this Git cheat sheat for my favourites.

No comments:

Post a Comment