Working with the version control system Git via GitHub has become standard for modern Android development. However, many beginners encounter difficulties already at the stage of connecting Android Studio to a remote repository. This article will help you understand all the nuances: from installing the necessary software to the first successful synchronization of the code.

We will look at two main authentication methods - by HTTPS and SSH, we will analyze typical errors (for example, Permission denied (publickey) or Failed to push), as well as We will give recommendations on organizing the project structure. We will pay special attention to the settings Android Studio Giraffe i Flamingo, where the integration interface with Git has undergone changes.

If you have not previously worked with version control systems, do not worry: the article is built from simple to complex. Experienced developers will find here the latest life hacks for optimizing workflow, including setting up .gitignore for Android projects and automating commits.

Preparation: what needs to be installed before connecting

Before starting integration, make sure that all the necessary components are installed on your computer. Without them Android Studio simply will not be able to interact with GitHub.

The first and most important thing is Git. Even if you use the built-in tools Android Studio, the system still relies on the local installation Git. You can download the current version from of the official website. During installation, leave all the default settings except one: in the Adjusting your PATH environment section, select the option Git from the command line and also from 3rd-party software. This guarantees that Android Studio will be able to find executable files Git.

The second is an account on GitHub. If you don't have it yet, register at github.com. To work with private repositories you will need paid tariff (only public projects are available for free). Students can get GitHub Pro for free through the app GitHub Student Developer Pack.

  • ๐Ÿ“ฅ Git - version control system (required!)
  • ๐Ÿ”‘ GitHub Account - account on the platform (registration takes 2 minutes)
  • ๐Ÿ›  Android Studio - version no lower Arctic Fox (2020.3.1)
  • ๐Ÿ”’ SSH key (optional, but recommended for security)

After installation Git check its operation in the terminal (or Git Bash on Windows) with the command:

git --version

If you see the version number (for example git version 2.40.1) - everything is installed correctly. If not, reinstall Git and make sure that the path to it is added to the environment variable PATH.

๐Ÿ“Š Which authentication method do you prefer?
HTTPS (login/password)
SSH keys
GitHub CLI
I donโ€™t know what it is

Setting up Git in Android Studio: first launch

Now that Git is installed, you need to configure it in Android Studio. Open your project (or create a new one) and go to File โ†’ Settings โ†’ Version Control โ†’ Git.

Here check two key parameters:

  1. Path to Git executable โ€” must point to git.exe (Windows) or the path to the binary in /usr/bin/git (macOS/Linux). If the field is empty, click on the ellipses button and specify the path manually.
  2. Update method โ€” select Merge (this is safer than Rebase, for beginners).

After that, go to File โ†’ Settings โ†’ Version Control โ†’ GitHub. Here you can log in via GitHub Account, but we recommend setting up Git manually first to understand the process. Click Add account and enter your data if you want to use HTTPSauthentication.

Important: if you work in a team, agree with colleagues commit style. In Android Studio this is configured in Settings โ†’ Version Control โ†’ Commit. We recommend using a template type(scope): subject (for example, feat(auth): add login button).

๐Ÿ’ก

Use a plugin GitToolBox for Android Studio โ€”it adds visual cues about the status of files directly in the code editor.

Creating an SSH key for secure authentication

SSH-keys is a more secure way to connect to GitHubthan HTTPS with a login and password. It eliminates the need to enter credentials each time push/pull and protects against. data interception.

To generate a key, open a terminal and run the command:

ssh-keygen -t ed25519 -C "your_email@example.com"

Replace your_email@example.com with the email associated with your GitHubaccount. The system will ask where to save the key - leave the default path (on Windows this is C:\Users\YourUser\.ssh\id_ed25519). You can also set a passphrase (passphrase) for additional protection.

Now you need to add the public key to GitHub:

  1. Copy the contents of the file id_ed25519.pub (you can open it in any text editor).
  2. Go to GitHub to Settings โ†’ SSH and GPG keys โ†’ New SSH key.
  3. Paste the copied key into the field Key, specify the name (for example, My Laptop) and save.

Check the connection with the command:

ssh -T git@github.com

If you see a message Hi username! You've successfully authenticated... โ€”everything is configured correctly. If not, check:

  • ๐Ÿ”‘ Whether the key was copied correctly (there should be no extra spaces or line breaks).
  • ๐Ÿ“‚ Whether the folder .ssh exists in the home directory.
  • ๐Ÿ”„ Has it been restarted? ssh-agent (on Windows this is done via Services).
What to do if SSH does not work?

If after all the manipulations the connection fails, try:

1. Remove old keys from ~/.ssh/known_hosts.

2. Use a different algorithm key: ssh-keygen -t rsa -b 4096 -C "your_email@example.com".

3. Temporarily disable the firewall or antivirus (they can block sshconnections).

Clone a repository in Android Studio

If you already have a repository on GitHub, it can be cloned directly into Android Studio. To do this:

  1. Select File โ†’ New โ†’ Project from Version Control.
  2. Insert the link to the repository in the field URL It can be in the format:
    • https://github.com/username/repo.git (For HTTPS)
    • git@github.com:username/repo.git (for SSH)
  • Specify the directory where to clone the project, and click Clone.
  • If the repository is private, Android Studio will request authentication. When HTTPS-connection, enter your login and password (or Personal Access Tokensince 2021 GitHub has disabled password support for Git-operations). SSH No additional actions are required - if the key is added correctly, cloning will take place automatically.

    After cloning, the project will open in Android Studio. Pay attention to:

    • ๐Ÿ“ Folder structure: make sure .gitignore is present and contains rules for Android projects (for example, excludes build/, .gradle/, local.properties).
    • ๐Ÿ”„ Tab Version Control (usually on the right) - changes in files.
    • ๐Ÿ“ค Button Commit (at the top of the panel) - it will become active as soon as you make changes.

    โ˜‘๏ธ Preparing for the first commit

    Done: 0 / 4

    First commit and push to a remote repository

    Now that the project is connected, you can make the first commit. Here is the step-by-step process:

    1. Enter changes to the code (for example, add a new Activity or change strings.xml).
    2. Open the tab Commit (Ctrl+K on Windows/Linux or Cmd+K on macOS).
    3. Select). files to commit (checkboxes on the left) Do not commit automatically generated files (for example, from build/).
    4. Write a commit message. It should be informative, for example: feat: add splash screen with animation or fix: resolve NPE in UserProfileFragment.
    5. Click Commit (or Commit and Pushif you want to immediately send changes to GitHub).

    If you selected Commit and Push, a window with push settings will open. Here you can:

    • ๐Ÿ”€ Select the branch (branch) to push to (usually main or master).
    • ๐Ÿ”„ Indicate whether to do push with a flag --force (dangerous, use only if you understand the consequences!).
    • ๐Ÿ“ Add a comment to the push (optional).

    Important: if you If you work in a team, never push directly to main/master. First create a separate branch (git checkout -b feature/my-feature), and then do Pull Request via the interface GitHub.

    Command Description When to use
    git add . Adds all changed files in staging When you need to commit all changes
    git commit -m "message" Creates a commit with a message After git add, before pushing
    git push origin branch Sends commits to the remote branch After a local commit
    git pull Downloads changes from the server and merges them Before starting work to avoid conflicts

    Common errors and their solutions

    Even experienced ones developers sometimes encounter problems when working with GitHub v Android Studio. Here are the most common errors and ways to correct them:

    1. Error: Permission denied (publickey)

    Reason: GitHub does not recognize your SSHkey. Solutions:

    • Check that the key is added to ssh-agent:
      eval "$(ssh-agent -s)"
      

      ssh-add ~/.ssh/id_ed25519

    • Make sure that the GitHub key is added to public the key (from a file with the extension .pub).
    • Try to generate the key again with a different algorithm (rsa instead of ed25519).

    2. Error: Failed to push some refs

    Reason: your local branch lags behind the remote one, and GitHub blocks the push to avoid data loss Solution:

    git pull --rebase
    

    git push

    If there are no conflicts, the rebase will take place automatically. If there are, resolve them manually (Android Studio will highlight conflicting files).

    3. Error: Authentication failed (with HTTPS)

    From August 13, 2021 GitHub disabled support for password authentication for Gitoperations. Instead of a password, you need to use Personal Access Token (PAT):

    1. Generate a token in Settings โ†’ Developer settings โ†’ Personal access tokens.
    2. Select scope: repo, workflow, admin:public_key.
    3. When pushing via HTTPS enter the generated token instead of a password.
    ๐Ÿ’ก

    Always use SSH or Personal Access Token for working with GitHub. Password authentication is no longer supported and will generate an error.

    โš ๏ธ Attention: If you are working on a corporate network, access to GitHub may be restricted by a firewall. In this case, contact your system administrator for proxy settings or use SSH via port 443 (add to ~/.ssh/config line Host github.com\n Hostname ssh.github.com\n Port 443).

    Optimization: useful plugins and settings

    Android Studio offers many tools to simplify working with Git. Here are some recommendations that will save your time:

    1. Plugins for Git

    • ๐Ÿ”ง GitToolBox - shows the status of files directly in the editor, adds visual hints for conflicts.
    • ๐Ÿ“Š GitLive โ€” allows you to see what files other developers are working on in real time.
    • ๐Ÿ” GitLink โ€” generates links to lines of code in GitHub (useful for code reviews).

    You can install plugins via File โ†’ Settings โ†’ Plugins โ†’ Marketplace.

    2. Setting .gitignore for Android

    The file .gitignore should exclude unnecessary files that should not be included in the repository. For Android projects, the minimum set of rules is:

    # Gradle
    

    .gradle/

    build/

    /local.properties

    IDE

    .idea/

    *.iml

    *.ipr

    *.iws

    Keystore

    *.jks

    *.keystore

    Local configuration

    secrets.properties

    The full template can be taken from official GitHub repository.

    3. Automation via git hooks

    Scripts pre-commit or pre-push allow you to automatically check the code before committing. For example, you can configure:

    • Code style check (ktlint for Kotlin).
    • Running unit tests.
    • Checking for the presence of hardcoded API keys.

    Example a simple pre-commit hook (save as .git/hooks/pre-commit):

    #!/bin/sh
    

    ./gradlew ktlintCheck

    ๐Ÿ’ก

    Use GitHub Actions to automate build and testing. Create a folder in the repository .github/workflows and add a file with the CI/CD configuration. It's free for public repositories!

    FAQ: answers to frequently asked questions

    Is it possible to connect Android Studio to GitHub without SSH?

    Yes, you can use HTTPS-connection. To do this, when cloning a repository, select a link like https://github.com/username/repo.git. 2021 GitHub requires using Personal Access Token instead of a password. You can generate a token in your account settings (Settings โ†’ Developer settings โ†’ Personal access tokens).

    How to change the message of the last commit?

    If the commit has not yet been sent to the server (push), you can fix it with the command:

    git commit --amend -m "New message"

    If the commit is already on GitHub, you will have to do revert or reset (but this is not recommended for shared branches).

    Why does Android Studio not see changes in files?

    Most often this happens due to:

    • Unsaved changes (click Ctrl+S).
    • Ignoring files in .gitignore (check the rules).
    • Cache errors Git (fixed by command git rm -r --cached ., but be careful!).

    Also try reloading the project (File โ†’ Invalidate Caches / Restart).

    How to undo the last commit?

    If the commit has not yet been sent to the server:

    git reset --soft HEAD~1

    This will undo the commit, but leave the changes in the files. If the commit is already at GitHub, use:

    git revert HEAD

    This will create a new commit, undoing the changes of the previous one (safe for team work).

    Is it possible to connect several GitHub accounts to one Android Studio?

    Yes, but for this you need:

    1. Generate separate ones SSH-keys for each account.
    2. Add them to ~/.ssh/config with different hosts:
    Host github-com-account1
    

    HostName github.com

    User git

    IdentityFile ~/.ssh/id_ed25519_account1

    Host github-com-account2

    HostName github.com

    User git

    IdentityFile ~/.ssh/id_ed25519_account2

    When cloning, use a modified URL:

    git clone git@github-com-account1:username/repo.git