You are on page 1of 124

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved.

1
Introduction

A successful creator publishes his or her work for the world to see. This is as true in software
development as it is in any other industry.

Publishing your code to a git repository has numerous advantages. The most obvious one, perhaps,
is the opportunity for other developers to make improvements to your code.

The less obvious, albeit equally important benefit is the reputation you build as a successful
programmer when you make your code public. Publishing your code to a git repository allows you to
gain credibility in the developer community. What’s more, if you’re actively looking for a new role
now or think you might be looking for one in the future, a portfolio of code will help you stand out to
employers. In my experience, it is the number one thing employers’ care about when hiring
developers – they want to be able to see your coding skills.

In this guide I’m going to show you how to use Android Studio--the fastest, best-supported
technology to build Android apps with GitHub--the world’s largest repository for open source
projects.

If you have any questions along the way, please feel free to ask them in the London App Developer
Facebook group: https://www.facebook.com/groups/1175349219195727

Let’s get started…

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 1
System Setup
If you haven’t done it already, the first step is to install Android Studio on your machine. Follow the
applicable link below for a step by step video explaining how.

Setting up Android Studio on a Mac

Setting up Android Studio on a PC

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 2
Table of Contents

Introduction
Table of Contents
Lesson 1: How to Push Your Project to GitHub
Step 1 - Create an Android Studio Project
Step 2 - Test the Project
Step 3 - Push Your Project to GitHub
Lesson 2: How to Clone a GitHub Project
Step 1 - Clone the Project
Step 2 - Making Changes
Step 3 - Commit Changes and Push to GitHub
Step 4 - View Changes on GitHub
Lesson 3: What Are Branches and How to Use Them
Step 1 - Creating a Branch
Step 2 - Switch between Branches
Step 3 - Rebasing a Branch
Merge and Rebase
Step 4 - Pushing a Branch to GitHub
Step 5 - Merging Changes
Lesson 4: Pulling Changes and Handling Conflicts
Step 1 - Make Change on GitHub
Step 2 - Update Android Studio Project
Step 3 - Create Conflicting Change
Step 4 - Update Project with Conflicting Change
Step 5 - Commit and Push to GitHub
Step 6 - View Changes in GitHub
Lesson 5: Forking Projects and Making Pull Requests
Step 1 - Fork the Project on GitHub
Step 2 - Clone the Project to Your Local Drive
Step 3 - Push Change to GitHub
Step 4 - Create Pull Request
Step 5 - Accept Pull Request
Lesson 6: Troubleshooting Common Problems
Issue 1 - Push Rejected
Issue 2 - Using GitHub Two Factor Authentication
Issue 3 - No Git controls at bottom of screen
Issue 4 - Missing options under VCS
Conclusion
About the Authors

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 3
Lesson 1: How to Push Your Project to GitHub
In the first lesson we will learn how to push our project to GitHub using Android Studio.

For the sake of consistency, we will create a brand new project from scratch which we will use
throughout this guide.

Click here for a full video demonstration.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 4
Step 1 - Create an Android Studio Project

Let's start by creating a new project to push to GitHub.

Open Android Studio and in the Welcome to Android Studio screen select Start a new Android
Studio project.

Note: If the above screen does not show, this is likely because Android Studio has been used on the computer
before. If this is the case, then just select File > New > New Project to create your project.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 5
In the New Project screen, type HelloGitHub as the Application name and click Next.

In the Target Android Devices screen, leave Phone and Tablet checked and click Next.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 6
Next, we will choose the default activity for our App (in Android development, each screen is
referred to as an "Activity").

In the Add an Activity to Mobile screen, select Empty Activity and click Next.

Note: Empty Activity is just a plain blank screen which will automatically insert some "Hello World" text.

In the Customize the Activity screen, leave the Activity Name as MainActivity and click Finish.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 7
Android Studio will now build your project...

When it is done, the project will open in Android Studio.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 8
Step 2 - Test the Project

Let's test our Android Studio project to ensure it's all working before we push it up into the cloud.

From the top menu, select Run > Run 'app' to run the app.

In the Select Deployment Target screen, click Create New Emulator.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 9
Next, we choose what type of device we wish to emulate. Choose the Category Phone and the
Nexus 5 device. Then click Next.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 10
On the System Image screen, leave it as Marshmallow x86 under the Recommended tab and click
Next.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 11
Leave the ADV Name as “Nexus 5 API 23” and all the other settings as they are. Click Finish to
create our Android Virtual Device.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 12
It may take a few seconds to create the Android Virtual Device...

When the emulator is created it should appear under Available Emulators. Select “Nexus 5 API 23”
and click OK to launch our app in this emulator.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 13
The emulator will load our app, displaying the words “Hello World!” on the screen.

Our App is working! Fantastic. You can now close the emulator and continue on to the next step.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 14
Step 3 - Push Your Project to GitHub

Note: Before you proceed, you must have your GitHub username and password handy. If you don't already have
an account, head over to https://github.com and set one up.

Now we are going to push our new project to GitHub.

From the top menu bar, select VCS > Import into Version Control > Share Project on GitHub.

You should be prompted to authenticate with your GitHub account. Enter your credentials in the
Login and Password boxes and click Login.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 15
Now we setup a Master Password that Android Studio will use to encrypt all the credentials it stores
on your hard drive. This does not need to be the same as your GitHub password. You will be asked
to enter this password each time you access any of the credentials that Android Studio stores
securely on your hard drive. If you have already done this then you will not be prompted to do it
again.

In the Setup Master Password prompt, enter and confirm a secure password that you will
remember forever and click OK.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 16
Next we will choose a name for our repository and enter a description that will appear on GitHub.

On the Share Project on GitHub prompt, leave the New repository name and Remote name default
and in the Description field enter "Android Studio GitHub Demo Project".

Note: If you get the message: "Repository with selected name already exists", this is because there is already a
GitHub repository with the name “HelloGitHub” on your GitHub account. To get around this, you can just enter a
different name in New repository name, for example: HelloGitHub2.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 17
The Add Files For Initial Commit screen will appear. This is where you can choose which files from
our project get synced to GitHub. Leave the selected file list and the Commit Message as default,
and click OK.

Note: In most cases you will want to include all the files that are listed. When Android Studio initializes the project
as a Git repository, it automatically creates a .gitignore file. The .gitignore file is a list of files that should be
excluded from the Git repository (typically these are files that Android Studio creates behind the scenes which are
only relevant to your local project). Any file listed in .gitignore is automatically excluded in the Add Files For Initial
Commit prompt.

If an Add File to Git prompt appears asking if you want to add "vcs.xml" to Git, click Yes. This is a
configuration file that is automatically created by Android Studio for our project. It is relevant to all
copies of the project, so we can safely add it.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 18
The Successfully shared project on GitHub message should appear. Click on HelloGitHub to view
your project in the browser.

You can now see all your Android project files. We will explain how to use this properly later, but for
now, feel free to have a browse to familiarize yourself with the GitHub interface.

That concludes the first lesson - How to Push Your Project to GitHub.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 19
Lesson 2: How to Clone a GitHub Project
This lesson follows on directly from Lesson 1. If you have not completed Lesson 1 yet, I recommend
going back and doing it now.

If you followed all the steps in Lesson 1, you will have created a HelloGitHub project and pushed it
to GitHub. In this guide we are going to close that project, remove it from our system and then clone
it again.

Click here for a full video demonstration.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 20
Step 1 - Clone the Project

Let's start by closing out the existing project. Make sure Android Studio is running and click File >
Close Project.

The existing project should close, and you will be presented with the Welcome to Android Studio
screen.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 21
From the Welcome to Android Studio screen, select the Check out project from Version Control
drop down menu, and choose GitHub.

The Enter Master Password prompt will appear. Enter the master password that you created in the
last lesson, and click OK.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 22
In the Clone Repository screen, expand the Git Repository URL dropdown menu. This will display
a list of all the projects in your GitHub account. Locate and select the one that ends with
"HelloGitHub.git".

We should receive a message saying “The path <project path> exists. Repository cannot be cloned
to an existing directory”. This is because we already have a project called HelloGitHub in our
workspace.

Leave the Parent Directory as it is, and change the Directory Name to "HelloGitHub2". Then click
Clone to clone the project.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 23
A message will appear confirming that you have checked out a Studio project and asking if you
would like to open it. This means that Android Studio has downloaded (cloned) the project files, and
detected that it is an Android Studio project.

Click Yes to open the project.

You can ignore the error messages that appear in the top right while the project is still loading.
These will sort themselves out after the project has been opened for the first time.

Note: It's not very obvious, but you can tell that Android Studio is still working on opening the project by the bar at
the very bottom of the screen.

After the project loads, you may be asked if you would like to add "vcs.xml" to Git. This is a file that
is created by Android Studio after the project has been checked out. Click Yes to add the file.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 24
The project is now open.

Let's run the project to ensure that it has been cloned correctly. Click Run > Run 'app' to test our
project.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 25
The Select Deployment Target menu appears. Select Nexus 5 API 23 and click OK.

If the App loads in the emulator, then all is working well!

You can minimize the emulator and keep it running for the next step.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 26
Step 2 - Making Changes

Next, we are going to make a change to our App. We will modify the text that is displayed to say
“Hello GitHub!”.

First, we need to add our desired text to the strings.xml file.

Expand the project explorer by clicking Project on the left of the screen. Then expand App > res >
values and double click on strings.xml.

Add a new string in between <resources> with the contents:

<string name="home_screen_text">Hello GitHub!</string>

Then click the save button (top left) to save the file.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 27
Next, we will modify our activity_main.xml file and tell it to take the home screen text from our new
string declaration.

In the project browser, expand Layout and double click activity_main.xml to open it. Then click
Text to edit the raw XML.

Near the bottom of our XML file, locate the line:

android:text="Hello World!" />

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 28
Change the "Hello World" text to "@string/home_screen_text". So the line should read:

android:text="@string/home_screen_text" />

Note: You may be wondering why we didn't just replace the “Hello World!” text with “Hello GitHub!”. The answer is
because the best practice is to use the strings.xml to store all text that is displayed in the App. This makes it easy
to translate the App into different languages and to use different layouts with the same content.

Let's test the change. Click Run > Run 'app' to test our App. The emulator should still be running
from the last step, so it should just load.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 29
The text should now read “Hello GitHub!”.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 30
Step 3 - Commit Changes and Push to GitHub

Let's commit our changes to our local Git repository.

From the official Git documentation (https://git-scm.com/docs/git-commit), a Git Commit does the
following:

“Stores the current contents of the index in a new commit along with a log message from the user
describing the changes”

In other words, it takes a snapshot of your exact code with a message describing the changes made
since the last snapshot.

Note: How often should you commit? That's a good question.

It's best to commit as often as possible to ensure you can revert back to previous versions if something breaks. If
you are making a small change (let's say, a couple of lines in 1 - 3 files), then you can commit after you made the
change. If your change is more complicated, then commit multiple times during the change.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 31
Now, let's commit the code in our project. Select VCS > Commit Changes...

The Commit Changes screen will appear. It contains a list of all the files that have been modified
since the last commit. As you can see, this includes the activity_main.xml and strings.xml files
which we changed. Also included are a couple of files in the .idea directory which are changed
automatically in the background by Android Studio. Leave all the files checked.

In the Author field, enter your name. Leave all the boxes under Before Commit as they are.

In the Commit Message box, enter a description of the changes we made by entering the following:

"Changed the home screen text from "Hello World!" to "Hello GitHub!" & Android Studio
changes."

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 32
Then click Commit to commit your changes.

Note: Commit Messages should contain a clear and concise description of what you changed. The details about
the files that you changed are included by default, so avoid duplicating this in the Commit Message by typing
things like "Modified the strings.xml file". Instead, describe the changes at a high level.

If you are working with a bug tracking system and have a ticket number which is related to the change you are
making, then you may wish to include the ticket number in the commit message.

Be disciplined when it comes to writing descriptive and punctual commit messages. This demonstrated attention to
detail and will make you popular amongst your colleagues.

A note will appear at the bottom of the screen confirming the changes have been committed.

Now that our changes are committed to our local Git repository, we are ready to push to GitHub.

Select VCS > Git > Push to push out changes.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 33
How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 34
The Push Commits screen will appear. This will list all the commits made and the files modified.
Click Push to push the changes.

Android Studio will push your project to GitHub. You can see the status at the bottom of the screen.

You will see a Push successful message when the push is done.

Note: You may have noticed in the Commit Changes screen, there was an option to Commit and Push under the
Commit button. In the future you can use this to save time by performing the commit and push in one step.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 35
Step 4 - View Changes on GitHub

Now we have pushed our changes, let's view them on GitHub.

Load up your favourite browser and head over to https://github.com and login to your account. You
can see your repositories on the right side of the screen. Click on the HelloGitHub repository we
have been working with.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 36
Click on the 2 commits link at the top of the project file list.

A list of commits will appear. Click on our latest commit at the top of the list.

The differences between the latest commit and the last commit are displayed. There are two ways
to view the commits:

● Unified - All changes appearing horizontally with removed lines in red and new lines in
green.
● Split - All changes side by side with the old version on the left and the new version on the
right.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 37
Let's select Split.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 38
Scroll down to the changes we made to see the difference in the activity_main.xml and
strings.xml files. Also take note of the comments section at the bottom. This is the social aspect of
GitHub and is very useful if other developers have questions about the changes that were made.

Congratulations! You have just successfully commit changes to GitHub. Now on for our next
lesson…

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 39
Lesson 3: What Are Branches and How to Use Them
Branches are one of the most useful features of Git.

It allows you to "branch" off your main development stream to work on a feature or bug fix without
affecting the main development stream.

Here is an example of how it works. You have a master branch, which is the main development
stream. All the changes we have committed to our project so far have been done on the master
branch. So the master branch contains the code for an Android App that displays "Hello GitHub!" on
the screen in a text box.

Let's say we want to add a new feature to our App. For example: A button on the home screen that
we can tap on, and it displays a message on the screen.

We could start working on this and commit to the master branch as we go along. But what if,
halfway through working on the feature, someone reports a critical bug, and we need to drop what
we’re doing to fix it right away. We can either rush the button feature and fix the bug after, or discard
the changes and lose all the work we did on the button so far. Neither sound like fun...

This is where branches come in. Before we start working on our new button feature, we can create
a branch called add_button. This effectively takes the exact state of the master branch at the time
we created the add_button branch, and allows us to make changes to that state independently
from the master branch. So any changes we make on the add_button branch, are kept separate
from the master branch. It's as if we had copied and pasted all the project files to a new directory
and started working from that one instead.

Note: When you create a new branch, Git isn't actually making a copy, but rather a pointer to the last commit made
before we created the branch. This is what makes Git such a lightweight and popular version control system. You
can learn more about the technology behind Git branches here: https://git-scm.com/book/en/v2/Git-Branching-
Branches-in-a-Nutshell.

Now that we are developing our new feature on the add_button branch, if we need to drop what we
are doing to fix a bug, we can commit our latest changes to add_button and then switch back to the
master branch. The progress we made on the new feature will still be saved on the add_button
branch, and the master branch is exactly as it was before.

Click here for a full video demonstration.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 40
Step 1 - Creating a Branch

First, open our HelloGitHub2 project in Android Studio (if you have another project open, you can
click File > Close Project to close it, and then double click on HelloGitHub2 from the Welcome to
Android Studio screen.

When that's done, let's create a new branch called add_button. At the bottom of the screen, select
Git: master and click New Branch.

In the Create New Branch dialogue, enter add_button as the name of our branch, and click OK.

Note: Branch names cannot contain special characters or spaces. The best practice is to make a branch name
that is descriptive but not too long.

The branch created message should pop-up, and you will notice that Git: master has switched to
Git: add_button.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 41
Now that we are working on the add_button branch, let's start working on our change. We will start
by adding the text that will go inside the button.

Open the project navigator on the left hand side and navigate to and open app > res > values >
strings.xml.

Now let's add the string. Insert the following line into the strings.xml file.

<string name="home_btn_text">Click Me</string>

Then, save the file.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 42
Next, let's edit the activity_main.xml file to add the button to our view. In the project navigation,
select app > res > layout > activity_main.xml to open the file. Ensure that the text tab is selected.

We need to make a number of changes to the activity_main.xml file.

First, change the layout to LinearLayout. You do this by deleting "RelativeLayout" from within the
top bracket, and replacing it with "LinearLayout". The bottom bracket should change automatically.

Then you need to add the orientation. This is a requirement for the LinearLayout. In the top
"LinearLayout" declaration, add the following line:

android:orientation="vertical"

Next, modify the existing TextView to fill the full width of the layout, by replacing "wrap_content"
with "fill_parent".

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 43
Finally, add the new button below the TextView:

<Button
android:id="@+id/btn_activity_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/home_btn_text"
android:onClick="btnClicked"/>

When you are done, the activity_main.xml file should look like this:

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.mark.hellogithub.MainActivity">

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/home_screen_text" />

<Button
android:id="@+id/btn_activity_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/home_btn_text"
android:onClick="btnClicked" />

</LinearLayout>

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 44
Then save the file.

Let's test our progress. Click Run > Run 'app'.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 45
In the Select Deployment Target menu, choose Nexus 5 API 23 and click OK.

So you can see the button is there, but if you click it the App crashes. This is because we have
added the button but haven't defined what it should do yet...

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 46
Let's commit our progress so far. Click on VCS > Commit Changes...

In the Commit Message input box, type "Added a button to the home screen.". Then, in the
Author field, type your name and click Commit.

A Code Analysis warning will show, because we haven't finished making the change, so it has
detected some errors. We will fix this later, so for now, click Commit.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 47
Step 2 - Switch between Branches

So, we are halfway through adding the new button to our project.

Let's imagine that, all of a sudden, an urgent request comes in. It absolutely requires us to
immediately drop what we are doing and work on it.

What do we do? Should we build it on top of our half-finished button change? Or should we
manually undo our button change to add it in? Or maybe we should rush the button change, skip the
testing and pray that it will work?

None of the above sound ideal.

Fortunately, we can use Git branches to put aside our button change and work on the previous
version of our code, while still saving all the progress we made so far. All we need to do is switch
back to the master branch. Do this by clicking Git: add_button at the bottom and choose master -
> origin/master > Checkout.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 48
You may have noticed that the activity_main.xml file reverted back to its previous state. Don't
worry, our changes have not been lost, we have just temporarily switched back to the state the code
was in before we created the add_button branch.

Let's test the App to make sure it really is in its previous state. Click Run > Run 'app' to test our
app.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 49
Select the Nexus 5 API 23 option and click OK.

Our App is back to its previous state (without a button).

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 50
For reasons unknown, the highly urgent and critical change request is to change the title of the app
from "HelloGitHub" to "GitHub Demo App". Go ahead and use the project explorer to open the
app > res > values > strings.xml file.

Next, modify the app_name string to read "GitHub Demo App" to make the strings.xml file look
like this:

<resources>
<string name="app_name">GitHub Demo App</string>
<string name="home_screen_text">Hello GitHub!</string>
</resources>

Then click the save icon to save the changes.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 51
Before we commit, let's test our changes. Click Run > Run 'app' to test our changes.

Notice that the App loads and the title has changed.

Now let's commit our changes. Click on VCS > Commit Changes...

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 52
In the Author field, select your name from the dropdown box. In the Commit Message field, describe
our change by typing "Changed the app name". Then click Commit.

Now, let's push our changes to GitHub. Click VCS > Git > Push.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 53
Review what is going to be pushed, and click Push.

Note: Notice how none of our button changes are included. That is because when you do Git push, you are only
pushing the currently selected branch.

If you are prompted by the Enter Master Password popup, enter it and click OK.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 54
Step 3 - Rebasing a Branch

Now that we have made our urgent change, we can get back to working on our button. Let's select
Git: master > add_button > Checkout to switch back to our add_button branch.

Notice how the strings.xml file has switched back to how it was before we made the change on the
master branch (the app_name has switched back to "HelloGitHub"). So our next challenge is to
update our add_button branch with the latest changes from the master branch.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 55
Merge and Rebase

There are two ways to integrate the changes between different branches in Git: merge and rebase.

The merge command is the easier of the two, it simply combines all the commits from both
branches, keeping all the history, and makes a new commit for the changes.

The rebase command is a little more complicated but has the added benefit of keeping a cleaner
commit history, which your colleagues will thank you for. Instead of combining all the changes and
applying them as a new commit, it will replay all of our add_button commits on top of the latest
version of the master branch.

Put another way, imagine we made a copy of our add_button branch and called it
add_button_temp. Then, we delete the original add_button branch and made a new one starting
from the updated master branch. Then we went through all the changes in the add_button_temp
branch and applied them, one by one, to the new add_button branch. Finally, we delete the
add_button_temp branch. Now we are left with a new version of the add_button branch which
includes all of our master branch changes as well as the changes in add_button. This is effectively
what git rebase does.

Note: You should never use rebase on a branch that other people are working on as this can cause complications
in your project. If this is the case, it's best to just use merge to avoid any issues in your project.

Let's rebase onto our master branch. Click on Git: add_button > master -> origin/master >
Rebase onto.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 56
A Rebase Successful message will appear, and you will notice that the app_name string has been
updated to "GitHub Demo App". This is because rebase has applied all the commits we made to
the master branch to our add_button branch.

Now let's test our project again. Select Run > Run 'app' to run our App.

As you can see, the title is updated and the new button is included.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 57
Now we can complete the button change.

When we click the button, we want to make it pop-up with a message saying “Button Clicked!”. Use
the project navigator to open app > java > com.example.[yourname].hellogithub > MainActivity.

Note: [yourname] will be the name of the user who created the project.

Now, let's add our method for receiving the button click. Update your MainActivity.java file to add
the following method:

public void btnClicked(View v) {

Context context = getApplicationContext();


CharSequence text = "Button Clicked!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();

Expand the import section at the top and add the following imports:

import android.content.Context;
import android.view.View;
import android.widget.Toast;

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 58
When you are done, the file should look like this (except your package declaration will match your
name instead of mine):

package com.example.mark.hellogithub;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void btnClicked(View v) {

Context context = getApplicationContext();


CharSequence text = "Button Clicked!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();

}
}

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 59
OK, now let's test our app. Click on Run > Run 'app'.

When the App loads, click the button and the "Button Clicked" message will appear.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 60
Now that it's working, let's commit our changes. Click on VCS > Commit Changes, select your
Author and type a Commit Message like "Completed adding the button to the main screen".
Then click Commit.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 61
Step 4 - Pushing a Branch to GitHub

So far, we have kept our add_button branch in our local Git repository. We can push this change to
GitHub so others can see what we have been working on, as well as contribute and comment on the
change if they like. Click on VCS > Git > Push... to push our branch to GitHub.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 62
Here you can see the details of the commits we have made to the add_button branch. Click on
Push to push the changes to GitHub.

You will have to type your password in the Enter Master Password screen and click OK.

A message should appear confirming that the push was successful.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 63
Now, let's switch over to GitHub and see how it looks. Login to https://github.com and click on the
HelloGitHub project.

As you can see, there is a notification for a recently pushed branch called add_button. We will
explain pull requests a bit later in the course.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 64
Let's click on the branches button to view all of our branches.

Notice our new add_button branch has been added to the list under Your branches. Click on the
add_button branch to view it.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 65
Now you can view the add_button branch of the project. Click on Compare to compare the branch
with its parent (the master branch).

Have a browse through and notice that it describes each change we made, including the file that
was modified and the lines that were added or taken away.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 66
Step 5 - Merging Changes

Hopefully you can now see the benefit of branches. It gives us the ability to work on our project in
separate streams, allowing us to make and test changes without affecting the main project. This is
really useful when making changes to our project, or if you want to make a slightly different version
that you can use to test or demonstrate something to a user.

Now, let's go back to Android Studio and merge our add_button branch into our master branch to
bring the project up to date.

To do this we need to switch back to our master branch. At the bottom of the screen, click Git:
add_button > master -> origin/master > Checkout.

Before we merge, let's double check exactly what it is we are merging. Click on Git: master >
add_button -> origin/add_button > Compare.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 67
Here, we see a list of all the commits we made. Best practice would be to check each one to ensure
you know what you are committing. Let's check the first one as an example. Click on the first
commit, and double click on MainActivity.java to bring up the comparison.

Similar to what we saw earlier in GitHub, we can see a side-by-side comparison of the before and
after file. Personally I think the Android Studio version is a bit clearer and easier to read, but that's
personal preference.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 68
After you have checked all of the commits, close the Compare windows and click Git: master >
add_button -> origin/add_button > Merge to merge our changes.

A message confirming the merge has been completed will appear.

Now, let's test to ensure everything is working well. Click on Run > Run 'app' to run our app.

Test the App to make sure it's all working as it should be.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 69
Finally, let's push our updated master branch to GitHub. Click on VCS > Git > Push...

Review the Push Commits and click Push to push our branch.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 70
A message will appear confirming that the push was successful.

Congratulations! That concludes our guide on using Git Branches.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 71
Lesson 4: Pulling Changes and Handling Conflicts
So far, we have done lots of pushing to GitHub but one thing that we haven't covered yet is pulling
updates from GitHub. GitHub is a collaboration tool which allows you to work efficiently with other
developers. This means that others with access to the repository can also push and make changes
to the project.

Click here for a full video demonstration.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 72
Step 1 - Make Change on GitHub

Let's start by pulling changes to our Android Studio project. To do this, we will make a change
using the GitHub interface to simulate someone else making changes to the project.

Log into https://github.com and navigate to the HelloGitHub project.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 73
Now use the project explorer to navigate to app > src > main > res > values > strings.xml.

Now that you have the strings.xml file open, click on the Edit this file button (pencil icon) to edit in
the GitHub editor.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 74
The file will open in the GitHub editor.

Insert a new string at the bottom of the resources tag:

<string name="test_string">String to Test Git Pull</string>

When you have inserted the string, scroll down to the Commit changes section. Type a commit
message "Added a string to test Git pull". Ensure Commit directly to the master branch is
selected and click Commit changes.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 75
Step 2 - Update Android Studio Project

Now that we have simulated another developer making changes to the project. Let's pull the
changes down to our Android Studio project.

Open Android Studio. If you have another project open already, click File > Close Project to close
it. Then click on our HelloGitHub2 project.

When the project opens, click on VCS > Update Project... to update our project.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 76
The Update Project dialogue appears. Under Update Type you can see the two options we
discussed in the previous lesson: Merge and Rebase.

The differences between Merge and Rebase were discussed in Lesson 3. Rebase should never be
used on a branch that multiple developers are committing to.

When you pull changes in Git, any uncommitted changes that have been made will be lost. If you
are working with Git outside of Android Studio, before you update your project you would need to
either commit or stash your changes. Git stash is a feature that allows you to locally store unfinished
changes which can later be merged and committed.

Fortunately, Android Studio makes dealing with these uncommitted changes easy. The Clean
working tree before update option lets you tell Android Studio what to do with any changes that
have not yet been committed. You can either use Stash or Shelve. The difference being that Stash
is a feature of Git, and Shelve is a feature of IntelliJ (the IDE that Android Studio is built on top of).

There is no significant difference in using Stash vs Shelve, except that Stash uses Git and Shelve
uses the IDE. My recommendation is to always use Stash, because that way if your IDE ever
crashed and became unusable you can still easily access your stashed changes using Git's
command line interface.

Under Update Type select Merge, then, under Clean working tree before update, select Using
Stash.

Click OK to update the project.

A message will appear at the bottom of the screen confirmation that the update has finished, and
listing the files that were updated.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 77
In the project explorer, navigate to app > res > values > strings.xml. You will see that the line we
added in GitHub has been added.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 78
Step 3 - Create Conflicting Change

Now imagine you are collaborating with someone on a project, and you both make changes to the
same lines of code. The changes are similar, but not identical. What happens when you try to
update your project with their changes? Or vice versa?

This is called a conflict. Let's simulate another developer making a change to our project by editing
through GitHub.

Go back to GitHub where we had opened the strings.xml file.

At the end of the test string we added, append "123" to make it read:

<string name="test_string">String to Test Git Pull 123</string>

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 79
Scroll down to the bottom and type a commit message "Changed the test_string".

Now that we have simulated another developer making a change, let's go back to Android Studio
and make a different change to the same line.

Open Android Studio and, at the end of our test string, append "abc" making the full line read:

<string name="test_string">String to Test Git Pull abc</string>

Then click the save button to save the changes.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 80
Step 4 - Update Project with Conflicting Change

Now, let's update our project. Click on VCS > Update Project...

The Update Project screen appears. As we did before, under Update Type choose Merge and
under Clean working tree before update choose Using Stash. Then, click OK.

The Files Merged with Conflicts box will appear. This is because a conflicting change has been
detected in the strings.xml file.

You have the option to click Accept Yours - accept your local changes and discard the remote
changes, Accept Theirs - discard your changes and accept the remote changes. However, best
practice is to review the changes and manually merge the files, to ensure nothing gets missed out.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 81
Click on Merge to merge the changes.

The Merge Revisions screen will appear. This is laid out like this:

● Left - Your local version of the file (changes that you made).
● Center - The resulting file that will be applied.
● Right - The remote version of the file (changes the other developer made).

Your goal is to combine the changes from the left and right screens into the centre screen, in a way
that keeps the code functioning correctly.

Android Studio will highlight the differing lines of code in red.

Let's keep our change to the code and also keep the other developers version. Click on the double
arrows on the right of the line on the left.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 82
Next, click on the double arrows to the left of the right hand side of the screen to add the remote line
below the conflicting line.

Now, since the string names must be unique, add a “2” to the end of the second test_string. Then
click Apply to confirm the changes.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 83
The Merge Revisions screen will close and you will see the new strings.xml file. The VCS Updated
message will be displayed.

Notice how the resulting file matches that of the centre text box in the screen above.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 84
Step 5 - Commit and Push to GitHub

Now that we have updated our local project, let's commit our merged changes and push to GitHub.

Click on VCS > Commit Changes...

The Commit Changes window will appear. Under Commit Message describe our changes by
entering "Added test strings for demo purposes". In the Author input select or enter your name.
Then click Commit and Push.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 85
The Push Commits summary will display. Click on Push to push the changes.

In the Enter Master Password prompt enter your Password.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 86
Step 6 - View Changes in GitHub

Now switch back to https://github.com and navigate to our HelloGitHub repository.

Once you are on the project, navigate to app > src > main > res > values > strings.xml to view
our updated file.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 87
The files have now been merged on GitHub. Excellent!

You have just learned how to merge and push conflicting changes using GitHub and Android
Studio.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 88
Lesson 5: Forking Projects and Making Pull Requests
GitHub has a feature which allows you to "fork" an existing public project. This means you can make
a copy of someone else's public repository in your own GitHub account. You can then ask the
owner of the original repository if you can merge your changes with the main project.

In this lesson we will learn about forking a project, making changes and submitting pull requests.

To follow this lesson, you must first sign up for a second GitHub account. This means you will need
to use a second email address. In this guide, I will refer to your first GitHub account as Account A,
and your second as Account B.

Note: If you have a Gmail account you can use an address alias to sign up for a second GitHub account. Basically
just add "+2" before the @ symbol (example: mark305+2@gmail.com). Learn more here:
https://support.google.com/mail/answer/12096?hl=en)

For this guide, I will be using my two GitHub accounts:

● LondonAppDev - https://github.com/londonappdev
● LondonAppDevTwo - https://github.com/londonappdevtwo

Click here for a full video demonstration.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 89
Step 1 - Fork the Project on GitHub

First, head over to https://github.com/login. If you are already logged in then logout and login again
with your second account (Account B).

Now, let's locate our HelloGitHub project. Since there are lots of other projects on GitHub called
HelloGitHub, let's find it by searching for our other user account.

In the search box enter the GitHub username for your other account.

Note: It's important you enter your own username here. Later in this guide we will continue using your other
account, which requires that you have forked from your own account.

Enter your username and press enter.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 90
The search results will be displayed. Click on Users.

Select your other GitHub account from the list of results.

The user profile will load. Click on the Repositories tab to show all of the public repositories on the
account.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 91
Select the HelloGitHub project from the list.

The project page will load. As you can see, it shows all the same details that we saw when we were
logged into our other account (the owner of this repository). On GitHub, projects are made public by
default, so anyone can see all the code and commit history of the project.

Now let's fork the project. Click on Fork on the top right to initiate the fork.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 92
The project forking will begin...

Once the project is forked, you will see a screen similar to the one we saw before. You can see all
the project files and commits as we could see before, but this time there is a note saying forked from
LondonAppDev/HelloGitHub (this will be the same but with your username).

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 93
Step 2 - Clone the Project to Your Local Drive

Now that we have forked our project, let's make some changes.

Load up Android Studio and close any existing project by clicking File > Close Project.

The first thing we need to do is change the GitHub credentials in Android Studio to point to our
second GitHub account (Account B).

Once the Welcome to Android Studio screen is displayed, click on Configure > Settings to open
the settings.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 94
The Default Settings window will load. On the left hand side, navigate to Version Control > GitHub.

In the Login box, enter your second GitHub account username (Account B) and in the Password
box, enter your password. Then click Apply to make the changes.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 95
The Enter Master Password box will appear. As described before, this is the master password for
Android Studio's password manager which you created in the first lesson. Enter your password and
click OK.

Now, on the Default Settings screen, click OK.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 96
Back to the Welcome to Android Studio screen.

There seems to be a bug in Android Studio where it doesn't clear the cache from the previous
GitHub credentials after changing them. To get around this we need to exit and reload Android
Studio. Click on the X to exit out of Android Studio, and then load it up again.

After restarting Android Studio, let's clone our forked project.

Click on Check out project from Version Control > GitHub.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 97
The Enter Master Password popup will appear. Enter your master password and click OK.

The Clone Repository box will appear. Click the dropdown for Git Repository URL and select our
HelloGitHub project. Make sure you select the one that has your second GitHub account username
(Account B).

In the Directory Name box, add Forked to the end of the name, making it read
"HelloGitHubForked". Then click Clone to clone the project.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 98
The Checkout From Version Control popup will be displayed, if you would like to open the project.
Click on Yes.

The project will open. Some Error Loading Project messages may be displayed while the project is
loading. Wait patiently and these should disappear once Android Studio has processed the project
correctly.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 99
Once the project loads, let's test it. Click on Run > Run 'app' to run the project.

In the Select Deployment Target screen, select Nexus 5 API 23 and click OK.

The emulator will load and you should see that the App is working.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 100
Step 3 - Push Change to GitHub

Let's make a change to our App. We are going to change the top bar colors from blue to green.

In Android Studio, click on Project on the left of the screen to open the project explorer. Then
navigate to app > res > values > colors.xml to open the colors.xml file.

Let's change colorPrimary and colorPrimaryDark. Update the file to look like this:

<?xml version="1.0" encoding="utf-8"?>


<resources>
<color name="colorPrimary">#3fb57a</color>
<color name="colorPrimaryDark">#329061</color>
<color name="colorAccent">#FF4081</color>
</resources>

Then click Save to save the file.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 101
Now let's test our app. Click on Run > Run 'app'.

You can see that the colors have changed.

Now we will push this change to GitHub. Click on VCS > Commit Changes...

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 102
The Commit Changes box will appear. In the Commit Message enter "Changed the colors of the
top bar from blue to green." In the Author field enter your name. Then click Commit and Push...

The Push Commits window will appear. Click on Push to push the commits.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 103
If the Enter Master Password appears, enter your password and click OK.

The Push Successful message will appear.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 104
Step 4 - Create Pull Request

Now that we have made our change, we can make a pull request. In GitHub, a pull request is a way
to request that the changes we made be merged into the project we forked from.

Switch back to GitHub and refresh the project page. Take note of the message which says "This
branch is 1 commit ahead of...". This is because we have made 1 commit to our project which has
not been updated in the main project.

Let's request the changes be merged. Click on Pull request to create a new pull request.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 105
The Comparing changes screen displays all the differences that we requested to be merged into the
main project. It shows all the files that have been changed and will be merged into the main project
(if the pull request is accepted). There are three files in total which are the colors.xml file and two
others which were changed automatically by Android Studio.

Scroll down and have a look over the changes. Then click Create pull request.

The Open a pull request screen will appear. This will automatically set the title to the last commit
message that was applied to the project. You can modify this and add a comment if you like.

Note: When making pull requests on real projects, you will want to write a descriptive comment explaining what
you changed and why it should be applied to the project. This will improve your chances of getting the pull request
approved by the project owner.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 106
Click on Create pull request to submit the request.

A new pull request is created. The page will be redirected to the Pull requests page. This is where
you the project owner and anyone else can contribute to the discussion regarding the pull request.
This is great because it allows for multiple developers to review and discuss the changes before
they’re applied, helping to catch bugs and suggest improvements before merging with the main
project.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 107
Step 5 - Accept Pull Request

Now the pull request has been created, we can switch over to our other GitHub account (Account A)
and view the request.

Click on your profile on the top right and select Sign out.

Click Sign in and login with Account A.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 108
Once you have signed in, you should see the pull request appear in your GitHub feed. Click on the
HelloGitHub project to open the project.

When the project opens, select the Pull requests tab.

Click on the "Changed the colors of the top bar from blue to green." pull request to view it.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 109
In the pull request page, click on the Files changed to view the files that will be merged in.

If you scroll down you can see the differences in the colors.xml file.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 110
Scroll back up and click on the Conversation tab.

Click on Merge pull request.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 111
At this point you can customize the comment that goes with the pull request. Click on Confirm
merge to confirm the changes.

The changes will be merged into the project. You will see that the status of the pull request goes
from Open to Merged. Click on Code to go back to the project's code view.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 112
You can see that the changes have been merged into the master branch.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 113
Lesson 6: Troubleshooting Common Problems
This section will go through a lot of the common issues people have when using Android Studio and
GitHub and how to resolve them.

Click here for a full video demonstration.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 114
Issue 1 - Push Rejected

The most common issue that GitHub & Android Studio newcomers experience is the "Push
Rejected" message.

This usually happens because changes have been made on the master branch which are not yet
pulled into your local branch.

To resolve this, simply pull the latest changes to your repository by clicking VCS > Update Project.

Then, in the Update Project prompt, select Merge, Using Stash and OK.

You should now be able to push the repository again.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 115
Issue 2 - Using GitHub Two Factor Authentication

Two Factor Authentication is a feature of GitHub which improves the security on your account by
requiring an extra step when logging into your account. So each time you authenticate, you need to
provide a password and a verification code which is sent to your phone. You can enable it here:
https://github.com/settings/security.

After enabling Two Factor Authentication in GitHub, you will start to receive the Authentication failed
message.

At the time of writing this guide, Android Studio doesn't support Two Factor Authentication as it
doesn't prompt for the second verification code.

A workaround is to create a Personal Access Token in GitHub. This token will replace the password
you enter in Android Studio when authenticating with GitHub.

Login to GitHub at https://github.com. Then click on your Profile Picture > Settings.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 116
In the Personal settings navigation, select Personal access tokens.

In the Personal Access Tokens screen, select Generate new token.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 117
In the New personal access token screen, enter the description Android Studio and check the
repo box.

Scroll down and select Generate token to generate the token.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 118
Once the token is generated, select the Copy Token button next to our newly created token.

Now, head back to Android Studio and click on VCS > Git > Push.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 119
On the Push Commits screen, click on Push.

You will be prompted to enter your password. Instead of entering your regular password, paste the
Personal Access Token that we copied to our clipboard. Make sure Remember password is
checked and click OK.

Note: If you do not get prompted to enter a password, this could be because the incorrect password has been
cached in the current session. To get around this, exit and reload Android Studio to refresh the cache.

Android Studio will now authenticate with your token instead of your password and you can continue
to work as normal.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 120
Issue 3 - No Git controls at bottom of screen

A number of the steps in the guide require that you make changes using the Git control at the
bottom of the screen.

In some cases, the Git control may not be visible.

Fortunately this is an easy fix. It's caused by the Status Bar being disabled. Simply click on View >
Status Bar to enable it:

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 121
Issue 4 - Missing options under VCS

Some people may experience some missing options from the VCS menu.

This is how it should look:

This is because Android Studio is not detecting that the project has been initialized as a Git
repository.

I have seen this happen a number of times while my project is still loading. If you have cloned the
project from GitHub, or you have already added the project to GitHub, then Android Studio may still
be loading the project in the background. Wait for about a minute, and if the options still do not
appear, exit Android Studio and reload it.

If you have not already added the project to GitHub, then follow the steps from Lesson 1 to do this.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 122
Conclusion

I hope you found this guide useful and now feel more confident when working with Android Studio
and GitHub. Mastering how to use GitHub with different technologies has made a huge difference to
my career development as a programmer. I’m confident you will find the same, too.

If you have any questions or feedback, please feel free to email me at


mark@londonappdeveloper.com or join the London App Developer Facebook group:
https://www.facebook.com/groups/1175349219195727

About the Authors

Written and co-produced by Mark Winterbottom. Mark is a full-stack software engineer and the
creator of London App Developer – a software consultancy firm and education blog. His work has
been featured in the Daily Mail, Mashable, TechCrunch, the Huffington Post, the Mirror and
Gizmodo, and over 30,000 developers read his programming tutorials every month.

Designed and co-produced by Brooke Rutherford. Brooke is a brand and marketing consultant for
London App Developer. She has led world-class marketing campaigns for Monsoon Accessorize,
Hult International Business School and Three UK.

How to Use GitHub with Android Studio © 2016 London App Developer Ltd. All rights reserved. 123

You might also like