How to commit and push to github from android studio

Before we dive in, let’s make sure you have the necessary prerequisites:

Android Studio installed

Familiarity with Git concepts (committing, branching, merging)

Step 1: Setting up Git

First, we need to set up Git on our machine. If you haven’t already done so, download and install the latest version of Git from the official website: Git – Downloads. Once installed, open a terminal or command prompt and run the following commands to check if Git is properly installed:

<h2>git --version</h2>
<h2>git config --list</h2>

Step 2: Creating a GitHub Repository

Next, we need to create a GitHub repository to store our code changes. To do this, follow these steps:

  1. Log in to your GitHub account and navigate to the “Repositories” section.
  2. Click on the “New” button to create a new repository.
  3. Enter a name for your repository (e.g., my-android-app) and select a visibility level (public, private, or organization).
  4. Select the appropriate repository template based on your project’s needs. For example, if you’re working on an Android app, select “Android App” from the dropdown menu.
  5. Click “Create Repository” to create your new repository.

Step 3: Connecting Android Studio to GitHub

Now that we have a GitHub repository set up, we need to connect Android Studio to it. Follow these steps:

  1. Open Android Studio and go to View > Tools Windows > Git.
  2. Click on “Git Settings” at the bottom of the window.
  3. How to commit and push to github from android studio

  4. Scroll down to the “System Settings” section and click on “Add Path”.
  5. /usr/local/bin (or the location where Git is installed on your machine)
  6. Click “OK” to close the window.
  7. Go back to the “Git Settings” window and click on “Add SSH Key”.
  8. Follow the prompts to generate a new SSH key pair. You’ll be asked for a name and a location where you want to save the key file.
  9. Once you’ve generated the key, copy the public key string to your clipboard.
  10. Go back to GitHub and navigate to your repository’s settings page.
  11. Scroll down to the “SSH Keys” section and click on “Add SSH Key”.
  12. Paste the public key string into the text box and give it a name (e.g., my-android-app).
  13. Click “Add Key” to save your changes.

Step 4: Committing Changes to the Local Branch

Now that Android Studio is connected to GitHub, we can start committing our code changes to a local branch. Follow these steps:

  1. Open your Android Studio project and go to View > Toggle Sidebar.
  2. Click on “VCS” (short for Version Control System).
  3. Click on “Branches” at the bottom of the window.
  4. Make sure you’re working on the branch where you want to commit your changes (e.g., main, feature1, hotfix). If not, switch to the correct branch by clicking on it in the list.
  5. In the right-hand sidebar, click on “Local” (short for Local Repository).
  6. Click on “Commit All” to stage all changes in your project and open the commit window.
  7. Enter a commit message in the “Message” field at the top of the window. Your commit message should summarize the changes you made and include any relevant information (e.g., bug fixes, new features).
  8. Click on the “Commit” button to create a new commit.
  9. Android Studio will prompt you to resolve any merge conflicts that may have arisen due to your changes. Follow the prompts to resolve these conflicts before committing again.
  10. Once you’ve resolved all conflicts, click on “Commit All” to stage all changes and open the commit window once more.
  11. Review your commit message and make any necessary changes before clicking on the “Commit” button.

Step 5: Pushing Changes to the Remote Repository</