Thursday, August 25, 2022

How to Use Git in Android Studio

In this blog, you will learn how to use Git in Android Studio. Firstly, you will learn the basic concept of Git and then create a new project using Git. Next, you will learn how to clone and fork a repository. Next up, you will see how to push your changes online using Github.  Also you can learn:
  • Integrating Git into the project
  • Setting up remote connections
  • Staging and committing changes
  • Pushing the changes to remote
  • Fetching the changes from remote
  • Working with branches
  • Showing log history

Note: This article uses Android Studio on a Windows OS machine for all references to paths, options, names, and configurations. Although it might alter significantly on other operating systems like Mac or Linux, the concept will remain the same.

Integrate Git into the project
Test whether Git is configured
In Android Studio, go to Android Studio > File > Settings > Version Control > Git. Click Test to ensure that Git is configured properly in Android Studio.

Enable version control integration
Suppose you’ve just created an Android project. In Android Studio, go to VCS > Enable Version Control Integration. This option won’t be visible if it’s integrated with any version control before.


Select Git version control system. On a successful enable VCS, a default local master branch will be created.

Add .gitignore to exclude files from Git
When you create a new Android project with Android Studio, two .gitignore files are automatically added (one in the project root folder, and the app folder). Files like generated code, binary files (executables, APKs), local configuration files should not be added to Git. Those files should be excluded from version control. Here’s my initial .gitignore file content:
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
File extensions that you want to keep outside the git track can be added. 
Stage and Commit Changes
The project is ready to use with Git version control. To stage and commit your changes, go to Git> Commit.
You will get a dialog to examine all files that will be added, enter commit messages and commit. You can deselect any files that you don’t want to belong to this commit.
When you click commit, a popup shows that you haven’t configured your username and email yet. You should always configure them because they will be attached to your commit message.
All done — now the whole project has been added to Git.
Set Up Remote Connections

To add the project to the remote repository, go to  Git > Manage Remotes :



Click on + and add the remote repo URL . Your local project is now linked to your remote Github repository. You can utilize Bitbucket, Gitlab, or any repository in addition to Github.

Push the Changes to Remote

To push your local changes to the remote repository, go to Git > Push.

The “Push Commits” popup will display which commit will be pushed to the remote-tracking branch. You can go ahead and make the push.
Fetch the Changes From Remote
To download the latest changes from remote, go to Git > Pull.

Work With Branches
Git's branching approach is referred to as its "killer feature" by some, and it undoubtedly distinguishes Git from other VCS systems. I'll demonstrate how to use branches in Android Studio in this part.
Create a new branch

Go to Git > Branches

“Git Branches” appears. It shows all the local branches and remote branches as well as the “New branch” option. If you click on New branch, you can create a new branch.
You can have a lot of options with the existing branch

  • Checkout: checkout master branch.
  • Checkout As: checkout new branch from master
  • Checkout and Rebase onto Current: checkout master and rebase feature branch onto it.
  • Compare with Current: commits that exist in master but don’t exist in feature and vice versa.
  • Show Diff with Working Tree: show the difference between the master and the current working tree.
  • Rebase Current onto Selected: rebase master on feature.
  • Merge into Current: merge master into feature.
  • Rename: rename the master branch.
  • Delete: delete the master branch.

You can choose the right option based on your needs. 
Show Log History
Go to Git > Show History.

It shows the log 



No comments: