Using Atlassian bitbucket with Intellij-idea

In this tutorial, we will go through the steps necessary to setup and use the Atlassian Bitbucket repository with your favourite Jetbrains IDE. The tutorial is based around Intelli-J Idea, however the steps will also work for PHPStorm, PyCharm, Android Studio and also from the plain old command prompt. The total setup time should be around 15 minutes or less.

Let’s get started

Download and Install the Bitbucket Connector Plugin

Download and Install  Git for Windows

  • Head to https://git-scm.com/download/win to download the latest version of Git for windows. (The download should start automatically, however you may also download packages for different architectures and configurations manually)
  • Run the installer, accept the license agreement.
Screenshots

    • Choose the items to install as per screen shots below (Hover the mouse on any screenshot to Zoom in on the image)  :-
    • Next >
    • Next >
    • Next>
    • Next>
    • Next>
    • Next>
    • Next>

  • Wait for the installer to finish, and then click the finish button

At this point, we have installed the Bitbucket connector for our Intelli-J IDE, and th Git tools for windows. If you have followed the steps above, the tools should have been automatically added to your system path variable.

Let’s test that out. Open a windows command prompt and type ssh-add -L

C:\>ssh-add -L
Could not open a connection to your authentication agent.

If you see the above prompt all is fine … don’t worry, we do not have our ssh-agent running yet, but the fact that the system knows where to find ssh-add, means that the path is set correctly.

Next step is to generate our ssh key pair.

Create the SSH Key

In order to authenticate ourselves with the repository, we can use an ssh public / private key. We will create this key pair, upload the public key to our bitbucket account, and use the private key on all the pc’s which will require access to the repository.

Let’s start by creating the ssh key.

Open a windows command prompt and type :

C:\>start-ssh-agent
Removing old ssh-agent sockets
Starting ssh-agent: done

Our ssh agent is now running

Lets create the key. Type in the command ssh-keygen -t rsa -b 4096 -C “your email”

C:\>ssh-keygen -t rsa -b 4096 -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/username/.ssh/id_rsa):bitbucket
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/username/.ssh/bitbucket.
Your public key has been saved in /c/Users/username/.ssh/bitbucket.pub.
The key fingerprint is:
SHA256:lZJUEWlYkN8qW0a6qHgtHadRHQ19scjQnlaOdhl9Eg 
[email protected]
The key's randomart image is:
+---[RSA 4096]----+
| .+BX=E .        |
| ..Xoo=o* o      |
| [email protected] o+ +        |
| . o @@ =..      |
| . = S o o       |
| o o . +         |
| o . .           |
| o . .           |
| ..o..o..        |
+----[SHA256]-----+

note : You may leave the name of the key unchanged (id_rsa) or you may call it something like bitbucket for future reference.

note : You may leave the passphrase blank, however for security purposes it is always better to create a passphrase. You will need to re-type this pass phrase every time you upload something to bitbucket, so make sure you do not forget it.

Create your first Bitbucket repo

  • Head over to https://bitbucket.org, and create a new account, if you do not already have one
  • Sign in to your new account, click on Repositories dropdown menu (top menu bar) and select “Create repository”. Make sure repository type is set to Git. Give a name to your repository, and click on advanced settings,  type in a description (if you like) and select “no forks” for the time being.
  • Click on create repository
  • You will now be greeted with your repository window
  • Click on your avatar on the top right corner of the window, and select “bitbucket settings”
  • Head down to securty (left column) and select ssh keys.
  • Click on Add Key
  • In the label, type in the name of the key (in this case we will use bitbucket)
  • and paste the contents of the file C:\Users\username\.ssh\bitbucket.pub, which we have created earlier. The contents you will be pasting should look something like this :-

  • Add the key, then click on repositories, and select the test repository we just created.
  • You can minimise your browser window for now.

Start your Intelli-J IDE, and open the project you want to upload to Bitbucket.

Open the terminal window of the IDE. (If you cannot see the terminal button, click on View and make sure “Tool Buttons” is selected.

In the ide’s terminal window type :-

C:\Your_Project>start-ssh-agent

you should see the following :-

Removing old ssh-agent sockets
Starting ssh-agent: done

If the agent does not start, check that you have installed the Git tools correctly, with the correct options, and that you have selected “Use Git and optional Unix tools from Windows command prompt” in the second screen of the Git tools installer

The keys we just created will be added automatically, however if you need to add these keys to another machine, you can do so by execution ssh-add C:\path_to_private_key\bitbucket. You will be asked to enter the passphrase, unless you had left this blank.

C:\Your_Project>ssh-add C:\Users\username\.ssh\bitbucket
Enter passphrase for c:\Users\username\.ssh\bitbucket:
Identity added: c:\Users\username\.ssh\bitbicket (c:\Users\username\.ssh\bitbucket)

At this point we are ready. In order to commit our first push, we need to execute the following commands in the ide’s terminal.

C:\your_project>git init
Initialized empty Git repository in C:/your_project/.git/
C:\your_project>git add .

At this point you might see several warnings about “LF will be replaced by CRLF in xxxxxxxxx

It is safe to ignore these warnings. Once the process finishes, it is time to add our remote repo.

Head over to the Bitbucket page on your browser and open the repo we just created (the one we called “My_Test_repo”). Repositories –> My_Test_Repo.

Click on “I have an existing project”

Copy the highlighted part (highlighted in the image above) and paste it into the terminal of the ide:

C:\your_project>git remote add origin ssh://[email protected]/yourlogin/your_project.git

Next we commit our changes, by issuing the command “git commit” :-

C:\your_project>git commit -m "initial commit"

And finally we push the commit to the Bitbucket repo :-

C:\your_project>git push --set-upstream origin master
Counting objects: 1796, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (1632/1632), done.
Writing objects: 15% (277/1796), 2.19 MiB | 14.00 KiB/s

Depending on the size of the project, and the upstream bandwidth available, this could take a few minutes. Go grab yourself a coffee.

After the first commit is done, you will see your files under “Source” on your bitbucket repo page. From then onwards, you may use the VCS–>Commit Changes button (CTRL-SHIFT-K) from the IDE to commit the changes to bitbucket …

You could also use the command line if you prefer.

To see a list of changes since the last commit use :-

C:\your_project>git status

To Commit the changes :-

C:\your_project>git commit - m "Minor Tweaks"
[master xxxxxxxxx] Minor Tweaks
 2 files changed, 312 insertions(+)
 create mode 100644 .idea/workspace.xml

To Push to the server :-

C:\your_project>git push
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 3.67 KiB | 0 bytes/s, done.
Total 5 (delta 2), reused 0 (delta 0)
To ssh://bitbucket.org/username/your_project.git
 xxxxxxx..yyyyyyy master -> master

and that’s it.

You can now fork, share and access your source code from anywhere, and also rest easy at night in case of a hard disk failure … your source code is safe inside the bitbucket repo !

 

Leave a reply:

Your email address will not be published.