Importing an Android Studio project in Git

1.0 Introduction

Importing an Android Studio project into Git is easy. First create the project or import an existing project in Android Studio. In this case a placeholder project HelloWorld was created. The directory listing of the project root directory looks like this.

$ ls -lsa
total 60
4 drwxrwxr-x 6 user1 user1 4096 Mar 31 17:09 . 
4 drwxrwxr-x 4 user1 user1 4096 Mar 31 17:02 ..
4 drwxrwxr-x 5 user1 user1 4096 Mar 31 17:07 app
4 -rw-rw-r-- 1 user1 user1  436 Mar 31 17:02 build.gradle
4 -rw-rw-r-- 1 user1 user1   81 Mar 31 17:02 .gitignore
4 drwxrwxr-x 3 user1 user1 4096 Mar 31 17:02 gradle
4 drwxrwxr-x 3 user1 user1 4096 Mar 31 17:03 .gradle
4 -rw-rw-r-- 1 user1 user1  855 Mar 31 17:02 gradle.properties
8 -rwxrw-r-- 1 user1 user1 5080 Mar 31 17:02 gradlew
4 -rw-rw-r-- 1 user1 user1 2404 Mar 31 17:02 gradlew.bat
4 -rw-rw-r-- 1 user1 user1  828 Mar 31 17:07 HelloWorld.iml
4 drwxrwxr-x 5 user1 user1 4096 Mar 31 17:07 .idea
4 -rw-rw-r-- 1 user1 user1  426 Mar 31 17:02 local.properties
4 -rw-rw-r-- 1 user1 user1   15 Mar 31 17:02 settings.gradle

2.0 .gitignore file

Android Studio creates a .gitignore file in the project root directory. The contents are,

$ cat .gitignore 
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build

3.0 Make repository

With the project infrastructure in place, we make the repository.

$ git init 
Initialized empty Git repository in /home/user1/projects/HelloWorld/.git/
$ git add -n .
add '.gitignore'
add '.idea/.name'
add '.idea/compiler.xml'
add '.idea/copyright/profiles_settings.xml'
add '.idea/encodings.xml'
add '.idea/gradle.xml'
add '.idea/misc.xml'
add '.idea/modules.xml'
add '.idea/scopes/scope_settings.xml'
add '.idea/vcs.xml'
add 'HelloWorld.iml'
add 'app/.gitignore'
add 'app/app.iml'
add 'app/build.gradle'
add 'app/proguard-rules.pro'
add 'app/src/androidTest/java/in/softprayog/helloworld/ApplicationTest.java'
add 'app/src/main/AndroidManifest.xml'
add 'app/src/main/java/in/softprayog/helloworld/HelloActivity.java'
add 'app/src/main/res/layout/activity_hello.xml'
add 'app/src/main/res/menu/menu_hello.xml'
add 'app/src/main/res/mipmap-hdpi/ic_launcher.png'
add 'app/src/main/res/mipmap-mdpi/ic_launcher.png'
add 'app/src/main/res/mipmap-xhdpi/ic_launcher.png'
add 'app/src/main/res/mipmap-xxhdpi/ic_launcher.png'
add 'app/src/main/res/values-w820dp/dimens.xml'
add 'app/src/main/res/values/dimens.xml'
add 'app/src/main/res/values/strings.xml'
add 'app/src/main/res/values/styles.xml'
add 'build.gradle'
add 'gradle.properties'
add 'gradle/wrapper/gradle-wrapper.jar'
add 'gradle/wrapper/gradle-wrapper.properties'
add 'gradlew'
add 'gradlew.bat'
add 'settings.gradle'
$ git add .
$ git commit -m "First commit."
[master (root-commit) 8030b98] First commit.
35 files changed, 669 insertions(+)
create mode 100644 .gitignore
create mode 100644 .idea/.name
create mode 100644 .idea/compiler.xml
create mode 100644 .idea/copyright/profiles_settings.xml
create mode 100644 .idea/encodings.xml
create mode 100644 .idea/gradle.xml
create mode 100644 .idea/misc.xml
create mode 100644 .idea/modules.xml
create mode 100644 .idea/scopes/scope_settings.xml
create mode 100644 .idea/vcs.xml
create mode 100644 HelloWorld.iml
create mode 100644 app/.gitignore
create mode 100644 app/app.iml
create mode 100644 app/build.gradle
create mode 100644 app/proguard-rules.pro
create mode 100644 app/src/androidTest/java/in/softprayog/helloworld/ApplicationTest.java
create mode 100644 app/src/main/AndroidManifest.xml
create mode 100644 app/src/main/java/in/softprayog/helloworld/HelloActivity.java
create mode 100644 app/src/main/res/layout/activity_hello.xml
create mode 100644 app/src/main/res/menu/menu_hello.xml
create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher.png
create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher.png
create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher.png
create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher.png
create mode 100644 app/src/main/res/values-w820dp/dimens.xml
create mode 100644 app/src/main/res/values/dimens.xml
create mode 100644 app/src/main/res/values/strings.xml
create mode 100644 app/src/main/res/values/styles.xml
create mode 100644 build.gradle
create mode 100644 gradle.properties
create mode 100644 gradle/wrapper/gradle-wrapper.jar
create mode 100644 gradle/wrapper/gradle-wrapper.properties
create mode 100755 gradlew
create mode 100644 gradlew.bat
create mode 100644 settings.gradle

4.0 Register Version Control in Android Studio

We can register that the project is under version control in Git in Andoid Studio. Go to File -> Settings -> Version Control. In the right column, there is Unregistered Git root: <project root directory> and a button Add root. Click on Add root. Click OK to complete.