How to enable Version Control in Android Studio using Git & Github?
Github is one of the most widely used web-based version control systems, based on Git. It is a useful source code management tool, where you can keep track of versions of your app and all the changes between it.
Version control helps you keep a track of changes and rollback to a previous version if there are any errors in the current version. Github makes this easier by providing a means to compare the code between commits and also preview parts of the code online. Code uploaded to github are stored in public repositories (accessible to everyone). Private repositories are available at $7/month.
Android Studio comes inbuilt with a number of choices for Version Control Systems, with options to use Git as well as Github. To make use of Github, signup for an account at github.com. If you are a student, make sure to sign up for the Student developer pack, where you are eligible to use private repos.
Below are the steps on how to setup the version control using Git & github for your project:
- Once you have signed into your Github account, click on the plus icon next to your profile icon and choose new repository.
- In the following page, enter a repository name and description. You can choose the repo to be private or public. Be careful not to select the Initialize this repository with a README (otherwise you will not be able to push your code into the repo. You will then have to pull the code first and then push it).
- Once the repo has been created, you will then get a link to access it. Make a note of that link. So in this case, the link to the repo I created is: https://github.com/TheCoffee/alertdialogs.git
- Now head over to Android Studio. In your project, select VCS -> Import into Version Control -> Create Git Repository.
- In the window that popups, select the project directory for git init. This is the directory that will be synced with the git repo. So in my case, I have chosen my project AlertDialogs. Once you click on this, all the files in the Project pane will become red.
- Add your project directory to your local git repo. You can do this by right clicking on your project folder and selecting Git -> Add. Once you click on this, all the files in the Project pane will become green.
- Right click on your project and select Git -> Commit Directory… On committing, the files in the Project pane will return to their original color (black).
- Add a commit message. Click on Commit and select Commit…
- Confirm the commit. You can also review the warnings and errors if any and correct them before committing the git repo.
- On committing your project, right click on it. Select Git ->Repository->Push… This is to push the local git repository to Github.
- In the new popup, select Define remote. A window will popup asking for the Name and URL to the github repository. Paste the url of the git repository that you had copied in Step 3: https://github.com/TheCoffee/alertdialogs.git
- On clicking OK, the git repo will be verified and you will now be connected to the master branch. You can choose to push your repo to any other branch of your repo by selecting master and changing it here.
- Push the repository to Github. You will get the following message on successful push. If the push fails, check that the github repo is empty. If not (if there is a README.md file), then right clicking on your project directory, choose Git->Repository->Pull. Then, repeat the process from Step 10.
- Now you can view your repository in your Github account. you will also be able to see the Commit message that you specified.
So now, you have a version control system in place for your project. With this system, keep track of the changes in your project. This system makes it easy to collaborate with others so that review your code and assist in the development process.