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:
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).
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]
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.
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.
Next, navigate to the projects location in explorer, right click the background and select Git Bash
The git bash prompt will appear.
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://email@example.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:
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.
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.
In the Commit Changes screen, confirm all your project files are selected by default, type a commit message and click Commit.
Next right click your project’s root directory (top left corner), and navigate to Git > Repository > Push.
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.
If all goes well, you should see this screen success message:
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.