Sunday, May 27, 2012

How to use bitBucket with EGit in Eclipse

Git is becoming more and more popular these days, and when we really use version control systems like Git or SVN, we actually want to share our codes with other developers. Thus, we do need a Git server to host the codes, like www.bitBucket.org, which is a Git server offering free limited use.

To set up a project in Eclipse, and push the project to bitBucket, you need to do the following steps:
(1) install EGit in Eclipse (http://www.eclipse.org/egit/);
(2) create an Eclipse project, e.g. HelloWorld; right click the project, and select Team->Share project... to add the project under Git control; right click the project again, and select Team->Add to index to add all the files of the project under version control; right click the project again, and select Team->Commit... to commit all the files;
(3) open an account on www.bitBucket.org, e.g. your account name is myaccount;
(4) configure the SSH in Eclipse:
click your project HelloWorld;
open menu Window->Preference->General->Network Connections->SSH2;
since now you have no SSH keys (bitBucket needs SSH keys for SSH authorization), select Key Management tab and click the button Generate RSA Key... (You can also use DSA keys);
then you can see the public key in the text area, and you need to copy the public key and save it in your account on bitBucket (Account->SSH keys); you also need to click the button Save Private Key... to save the private key to your local directory;
click the General tab, and click the Add Private Key... button to choose the private key that you just saved;
click the OK button to apply all the changes;
(5) on bitBucket, create a repository named HelloWorld, and then you can get the SSH address of the repository as:
ssh://git@bitbucket.org/myaccount/HelloWorld.git
(6)right click the project in Eclipse, and select Team->Remote->Push...;
then enter the SSH address and choose SSH as the protocol; Click the Next> button;
(7) click Add all branches spec button only, and then click the Next> button;
(8) click OK

Till now, other developers can clone the project resided on bitBucket, and they can also push changes to the repository.
However, although you can push changes to the remote repository, you cannot pull changes from the repository, since the pull operation is not configured to work with the remote repository.
To solve this problem, you have to add the following lines to the Git configuration file (in your eclipse project folder .git/config):
[remote "origin"]
url = ssh://git@bitbucket.org/myaccount/HelloWorld.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master

25 comments:

Unknown said...

Thanks works like a charm

ERS said...

Great post! Tks from Brazil!

Bradford said...

Perfect, thanks

Anonymous said...

I can't download egit in the link given. Is there any other link to download egit?

david said...

V. helpful. Although Egit complained about bitbucket's fingerprint not being recognised..

Thanks from London, England :)

Anonymous said...

I have the same problem and it doesn't work. So I am using HTTPS in step 6 (URL: https://accountname@bitbucket.org/accountname/reponame.git and password).

Thanks for the information!

FernandoJS said...

Great post!

Thanks!

tharapon said...

Thank you so much. Work like a charm!

Mariano said...

if you have problems in step 6 just give permissions like this:

chmod 600 /home/[your_user]/.ssh/[the key that you generated]

Zaan said...

Thanks a lot for this!

Worked on the first try.

Unknown said...

Thanks... Worked after googling some errors..

Shrenik Shah said...

Works great!
Even I used https in step 6 for authentication.
Thanks a lot for such a detailed explanation!

Ricardo Gaspar said...

Zé carlos!!

Thank you for your greate post!

but for the PULL fuction you missed something. Here it is:

[branch "master"]
remote = origin
merge = refs/heads/master

Jan1337z said...

Dont forget to restart eclipse after setting the ssh key.

Anonymous said...

I tried modifying the config file, i still can't seem to pull anything from bitbucket.

Anonymous said...

Helpful post; step (5) was helpful with http://stackoverflow.com/questions/13208868/how-to-push-a-new-project-in-github-from-eclipse and http://wiki.eclipse.org/EGit/User_Guide#Adding_a_Remote_Configuration and

Anonymous said...

Extremely helpful... i really couldn't make head or tail out of this anywhere else

Anonymous said...

Thanks!

Jordan D. Ulmer said...

Detailed but concise and methodical. Very well written post!

Thank you!



Sincerely,
-_-_-

“16 For God so loved the world that he gave his one and only Son, that whoever believes in him shall not perish but have eternal life.” ~ NIV John 3:16

Jordan D. Ulmer

Anonymous said...

Thanks !! But you need to add in .ssh/ the file known_hosts with this inside : bitbucket.org,207.223.240.182 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAubiN81eDcafrgMeLzaFPsw2kNvEcqTKl/VqLat/MaB33pZy0y3rJZtnqwR2qOOvbwKZYKiEO1O6VqNEBxKvJJelCq0dTXWT5pbO2gDXC6h6QDXCaHo6pOHGPUy+YBaGQRGuSusMEASYiWunYN0vCAI8QaXnWMXNMdFP3jHAJH0eDsoiGnLPBlBp4TNm6rYI74nMzgz3B9IikW4WVK+dc8KZJZWYjAuORU3jc1c/NPskD2ASinf8v3xnfXeukU0sJ5N6m5E8VLjObPEO+mN2t/FZTMZLiFqPWc/ALSqnMnnhwrNi2rbfg/rd/IpL8Le3pSBne8+seeFVBoGqzHM9yXw==

Piero said...

Thankyou very much from Italy!

Anonymous said...

One more "Thank you" from Brazil!

Mushiyo said...

Nice post!
Thank you!

Anonymous said...

Many thanks, you couldn't have made it any simpler or more obvious. Spared me doing homework

Unknown said...

I use HTTPS instead of SSH, but your solution still works. Thank you.