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.
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:
Path to Git executableโ must point togit.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.Update methodโ selectMerge(this is safer thanRebase, 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:
- Copy the contents of the file
id_ed25519.pub(you can open it in any text editor). - Go to GitHub to
Settings โ SSH and GPG keys โ New SSH key. - 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
.sshexists in the home directory. - ๐ Has it been restarted?
ssh-agent(on Windows this is done viaServices).
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:
- Select
File โ New โ Project from Version Control. - Insert the link to the repository in the field
URLIt can be in the format:https://github.com/username/repo.git(ForHTTPS)git@github.com:username/repo.git(forSSH)
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
.gitignoreis present and contains rules for Android projects (for example, excludesbuild/,.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
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:
- Enter changes to the code (for example, add a new
Activityor changestrings.xml). - Open the tab
Commit(Ctrl+Kon Windows/Linux orCmd+Kon macOS). - Select). files to commit (checkboxes on the left) Do not commit automatically generated files (for example, from
build/). - Write a commit message. It should be informative, for example:
feat: add splash screen with animationorfix: resolve NPE in UserProfileFragment. - Click
Commit(orCommit 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 (usuallymainormaster). - ๐ Indicate whether to do
pushwith 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 (
rsainstead ofed25519).
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):
- Generate a token in
Settings โ Developer settings โ Personal access tokens. - Select scope:
repo,workflow,admin:public_key. - When pushing via
HTTPSenter 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 useSSHvia port443(add to~/.ssh/configlineHost 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 (
ktlintfor 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:
- Generate separate ones
SSH-keys for each account. - Add them to
~/.ssh/configwith different hosts:
Host github-com-account1HostName 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