, ,

How to push to a remote Git repository over SSH with private/public key authentication using Android Studio

Android Studio is currently in Beta phase. However given that it is most likely to supercede Eclipse as the next Android IDE, I thought it would be a good idea to start using it now to develop my apps.

Like many software engineers, I use Git as my source control software. I have a set of Git repositories that I keep on a Digital Ocean cloud virtual machine. This serves as a central place to store my code as well as a backup in-case my hard disk fails or someone comes and steals my computer (knock on wood).

When I first started using Android Studio, it took me some time to figure out how to work from  a remote Git repository over SSH using private/public key authentication. As a result, I wrote this guide on how to do it.

At the time of writing this guide, I am using the following on my development machine:

Click here to download an easy-to-follow PDF version of this post

How to push to a remote git repository in Android Studio

Open explorer, browse to c:\users\[your username]\ and create a directory called .ssh (if I doesn’t already exist).

Windows Explorer .ssh Directory Windows Explorer .ssh Directory

This is where Android Studio will look for the configuration of your remote servers when you try and push/pull your repository.

Next create a new file inside your newly created .ssh folder and name it config.

Open the file with a text editor (I use Notepad++) and add the following:


Host     [server url]

         HostName             [server url]

         Port                  22

         IdentityFile          [location of your open ssh private key]

For example:

Example of ssh config file Example of ssh config file

The contents of this file is as follows:

  • Host – this is the name of your host. This can be anything but I think it’s best to use the fully qualified domain name of your remote server.
  • HostName – this is the FQDN of your host (in my case its always the same as the host).
  • Port – this is your port which your ssh deamon accepts connection on. The default is 22.
  • IdentityFile – this is the path of your private key in open SSH format. This is important if you use private/public key as its the only way Android Studio knows which key to use to authenticate you.

Now load up Android Studio and create or open a project.

Once you have a project open, navigate to VCS (from the top menu) > Import into Version Control > Create Git Repository.

Selecting 'Create Git Repository in Android Studio' Selecting ‘Create Git Repository in Android Studio’

On the Select directory for git init screen, choose where to initialize your local git repository (I usually leave the default select which is in the project directory) and click OK.

Selecting Git repository location in Android Studio Selecting Git repository location in Android Studio

Next, navigate to the projects location in explorer, right click the background and select Git Bash

Location of project files in Windows Explorer Location of project files in Windows Explorer

The git bash prompt will appear.

Git Bash in Windows Git Bash in Windows

Add a new git remote by typing:


git remote add origin ssh://[user]@[server_address]/[git_repo_url]

This will create a git remote named origin. Each part is broken down below:

  • The [user] should be replaced by a user on the server that has read and write access to the git repo.
  • The [server_address] is the FQDN of the server hosting your git repo.
  • The [git_repo_url] is the location of the git repo on the server.

An example of the command could be ssh://mark@londonappdeveloper.com/usr/repo/my_android_project.git.

Now add a file to the git repository by typing:


git add .gitignore

I do this is because we need to add any file to our git repo to make our first commit (and this is created automatically by Android Studio when you initialize the git repo). After this, we will add the rest of the files using the Android Studio interface.

Now commit the changes to the git repository using the comment “Initial Commit”:


git commit -m "Initial Commit"

Next, push to the remote using the flag –set-upstream to tell git that the destination repository will be the upstream one.


git push --set-upstream origin master

The output will look something like this:

Git bash commands complete Git bash commands complete

Press enter and then close the box.

In Android Studio right click your projects name(in the top left corner), then select Git and choose Add.

Adding all project files to git in Android Studio. Adding all project files to git in Android Studio

This will add all the files from the root of the project to your Git repository (the same as when we typing git add previously).

Next, right click the projects name again, select Git and choose Commit Directory.

Commit Directory option in Android Studio. Commit Directory option in Android Studio.

In the Commit Changes screen, confirm all your project files are selected by default, type a commit message and click Commit.

Selecting location for Git repository in Commit Changes screen Selecting location for Git repository in Commit Changes screen

Next right click your project’s root directory (top left corner), and navigate to Git > Repository > Push.

Selecting Git push in Android Studio Selecting Git push in Android Studio

The Git Push screen will appear. This will display the last commit that you are about to push. Click the Push button to send your changes to the remote git repository.

Git push screen in Android Studio. Git push screen in Android Studio.

If all goes well, you should see this screen success message:

Message for a successful push in Android Studio. Message for a successful push in Android Studio.

And that’s it. Every time you make changes you commit and push the updates to the remote Git repository.

As always, please leave comments below if you have any questions or ideas on how to improve this article.

If you want to download a PDF version of this guide for your records (which I always suggest as best practice), click on the link below and we’ll send a copy right to your inbox.

Cheers,
Mark

Click here to download an easy-to-follow PDF version of this post

6 Responses

  1. I have following error when pushing a fie
    remote: error: insufficient permission for adding an object to repository databa se objects remote: fatal: failed to write object error: unpack failed: unpack-objects abnormal exit

    Please help me to solve this

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: