This tutorial will show you how to setup an existing Android Studio project with Git.
Git is an open source version control system that is used in projects to share and handle code and other resources.
I’ve made this tutorial because everytime I start a new project in Android Studio, I never remember the flow of setting up a repository with the project. Note that with an already existing project you would only have to clone said project into Android Studio. This tutorial are concerned with setting up Git with a new Android Studio project. This tutorial is done on a Ubuntu 16.04 distribution and the use of tools like Git in the terminal might vary some if you use Windows or Mac.
First off create a new repository on whatever Git related service you might use. In this tutorial I’m using BitBucket but it really doesn’t matter. When your repository is created you will need to copy the link to it (it’s often found in the repository overview).
Now create a new Android Studio project with the name of your choosing. When that is done, open up a terminal and navigate to the newly created projects root (the project root is the uppermost folder in the project structure). When you are in the project root, type in following commands:
You should receive a message telling you something like:
Initialized empty Git repository in /path/to/your/projects/root/projectname.git
This initializes an empty local Git repository on your computer. This is only local right now and we will have to “connect” it to the repository created on whatever Git service you’re using. To do so type (with the link to the repository pasted in):
git remote add origin paste-your-link-here
This will “connect” the local repository to the repository created on the Git service, and thus enabling us to upload our code. Now open up Android Studio again with your project. If everything worked, then Android Studio will have recognized the existing repository and mark unversioned files red. (By unversioned files , I mean all files that are not yet added to Git for upload.
Now first select the project view  instead of the android view. After that right click the project root folder  and choose Git -> Add +. This will add all your unversioned files to the Git “staging”. I will not go into how git add, git commit and git push works but you can look it up here
Now click the commit button in Android Studio and select Commit and Push  in order to commit all your files. This will start the commit dialog window.
First off we need to add a commit message to the commit. Write something that will signify this specific commit (usually “Initial commit” works when starting a new project, otherwise describing what changes are done in the commit is good practice). Then press the Commit button in the lower right corner of the dialog window. This will in some cases bring up a warning popup dialog window informing you that some things in your code might need to be looked at before commiting. For the purpose of this tutorial ignore this warning as of this time (don’t do this later, check whatever warnings AS has for you and try to fix these before committing). Following this will bring up the next dialog option which represents pushing the committed files to the remote repository created on the Git service. (If you somehow only clicked Commit and not Commit and Push in the previous option, the dialog will exit. To then push your files use the key binding Ctrl + Shift + K or select VCS -> Git -> Push.
On the right hand side you are displayed with all your files that are going to get pushed to the remote repository. Click the Push button in the lower right corner to initiate the process. When the upload is done, check you Git service repository page (in my case BitBucket) and see that all your code and resources has been uploaded and is represented in the remote repository.
I hope you found this tutorial useful 🙂