Version Control System using Git on Android Studio

Akbarsha M
5 min readMar 5, 2016

--

Do you use git on terminal more? Then you must be missing the built in functionality of android studio which would increase your productivity time. Are you tired of typing those $git push and $git pull on your terminal very often? Lets dive into some of the useful git shortcuts on Android Studio

How to enable version control system to your project?

Click on the VCS menu then select Enable Version Control Integration to enable the git VCS.

Congratulations! You just enabled the git integration for your project. If this is the first time you’re enabling VCS for your project then you must be seeing all the files of your project in red color which means you’ve not committed anything to your project.

All the shortcuts below are using Android Studio’s Default Keymap

How to add a file to to your git?

To add a file to your git local repository Press Ctrl + Alt + A ( ⇧⌘A search git add)or Right click on the file you want to add then select Git -> Add. You can select the root folder of the project to add everything.

How to do a git commit?

To do a $git commit you press Ctrl + K ( ⇧⌘A search commit ) then select the list of files which need to be committed on the commit changes dialogue and then enter the commit message then click on the commit button to make a commit.

Commit options

This dialogue allows you to select the files which you would like to commit

  • This will show you new files in green, modified files in blue and deleted files in grayed out color.
  • Optionally you can select Reformat code, Rearrange code, Optimize imports and Clean up on the fly before you commit
  • A click on the commit button will do a normal commit until you press the little down arrow to select other options like commit & push or Create patch
  • Click on the Details at the footer to see the changes for each file. You can even double click on the file to see the changes. To go through each changes you’ve made for each file one by one Press F7 or click on the UP or DOWN inside the Difference Viewer Dialogue like below

For mac users, Since I don’t know about the shortcuts I would recommend you to press ⇧⌘A to find the appropriate shortcut and type the search term given inside the parenthesis in this post for each shortcut. It is an easiest way if you don’t remember all the shortcuts

How to do a git push?

To do a $git push press Ctrl+Shift+K ( ⇧⌘A search git push ), you will see the list of all your commit there. On the right side you can see the changes of the files you’ve made. Click on the Push button to make a git push to your remote repository.

Unfortunately at this point If you’ve not configured your remote repo to your git local repo, you can not add it using the IDE. You’ve to do that from your terminal. There is a feature request to jetbrains which is still open https://youtrack.jetbrains.com/issue/IDEA-87099

on terminal type $git remote add remoteName remoteUrl” to add remote repo as of now

Adding files to git, committing to git and pushing to remote might look like an easy job even from terminal at this moment. But the real power of Android Studio comes in when you take a pull with merge conflicts.

How to take a git pull?

Press Ctrl+T ( ⇧⌘A search git pull ) a pop dialogue which says Update project will ask you to select the Update type and will give you an option to Clean working tree before update using stash or shelve method like below

Update your project
  • Merge: choose this option to have the merge strategy applied. The result is identical with that of running git fetch ; git merge or git pull — no-rebase.
  • Rebase: choose this option to have the rebase strategy applied. The result is identical with that of running git fetch ; git rebase or git pull — rebase.
  • Branch Default: choose this option to have the default command for the branch applied. The default command is specified in the branch.<name> section of the .git/config configuration file.
Resolving conflicts

When you get a conflict, Merge revisions dialogue will appear with the list of conflicts like above with three options available to resolve your issues. This the life saving part of your Android Studio. You can discard all your changes if you select Accept theirs or you can keep only your changes discarding remote changes if you select Accept yours. If you click on the Merge button, changes from local repo on the left and changes from the remote on the right and a merge result on the middle will be shown in a separate dialogue

Merge conflicts

The green color changes are always safe to move from either side. The blue color changes are having modification on the existing statements on your code. The red color always has serious modification on both side which needs to be resolved carefully. The gray color shows you the deleted lines.

You can use the arrow heads to move the changes between local and remote repo until the IDE tells you merge conflicts have been processed. Click on the X button to discard any changes. You can click continue to save the current file and proceed to the next file to resolve the issues. Click on Apply to finish the rebasing.

If you fail to resolve the issues and close File merged with conflicts dialogue without any changes, the IDE will write all the conflicts to your local repo leading to an error state. This state is the middle of your rebasing usually. You can bring the dialogue back if you click on the notification Reolve All action

Updating and Pushing is easier on Android Studio rather than resolving the conflicts for 50 files on your terminal. Its a tedious job. In this post I’ve covered the basic push, pull for working branch. Next post we’ll see rebasing between different branches, Changelist and etc.

--

--

Akbarsha M
Akbarsha M

Written by Akbarsha M

¯\_(ツ)_/¯ try again, fail better, the world is yours | Tech CEO at Plattr Tech Studio | ex PayU | ex Colearn | ex-MPL | ex-BYJU’S

Responses (3)