Android Code Studio includes comprehensive Git integration powered by JGit, enabling full version control capabilities directly from your Android device.
Overview
The Git integration provides:
Repository initialization and cloning
Staging and committing changes
Branch management
Remote repository operations (push, pull, fetch)
Commit history viewing
Diff viewing and change tracking
.gitignore and .gitattributes support
Git operations are powered by JGit, a pure Java implementation of Git that runs natively on Android.
Getting Started
Initialize a Repository
Create a new Git repository for your project:
Open Git Menu
Navigate to Tools > Git or tap the Git icon in the toolbar
Initialize Repository
Select “Initialize Repository” to create a new Git repo
Set Default Branch
Choose your default branch name (e.g., “main” or “master”)
Configure User
Set your Git username and email for commits
# Initialize repository
cd ~/projects/MyApp
git init
# Set user configuration
git config user.name "Your Name"
git config user.email "[email protected] "
Clone a Repository
Clone an existing repository from a remote URL:
Select Clone
Choose Git > Clone Repository from the menu
Enter Repository URL
Provide the Git repository URL (HTTPS or SSH)
Choose Location
Select the local directory for the cloned project
Enter Credentials
Provide username and password/token if required
# HTTPS
https://github.com/username/repository.git
# SSH
[email protected] :username/repository.git
For GitHub, use a Personal Access Token instead of your password for HTTPS authentication.
Making Commits
View Changes
See what files have been modified:
Open Git Status
Navigate to Git > Status or view the Git panel
Review Changes
View modified, added, deleted, and untracked files
Check Diff
Tap on a file to see the detailed diff of changes
Change Types:
Modified - Existing files with changes
Added - New files staged for commit
Deleted - Files removed from the project
Untracked - New files not yet added to Git
Conflicting - Files with merge conflicts
Stage Changes
Add changes to the staging area:
Stage Specific Files
Stage All Changes
Stage by Pattern
# Stage a single file
git add path/to/file.java
# Stage multiple files
git add src/MainActivity.java res/layout/activity_main.xml
Create Commits
Commit staged changes to the repository:
Stage Your Changes
Ensure all desired changes are staged
Write Commit Message
Enter a clear, descriptive commit message
Commit
Tap “Commit” to save changes to the repository
Commit Message Best Practices
# Good commit message format:
Add user authentication feature
- Implement login screen
- Add password validation
- Integrate with backend API
# Short messages for simple changes:
Fix typo in MainActivity
Update dependencies to latest versions
Write meaningful commit messages that explain why the change was made, not just what changed.
Branch Management
View Branches
See all available branches in your repository:
# View local branches
git branch
# View all branches (local + remote)
git branch -a
Create Branches
Create new branches for features or bug fixes:
Open Branch Menu
Navigate to Git > Branches
Create New Branch
Select “New Branch” and enter a branch name
Switch to Branch
The new branch is automatically checked out
Create Branch
Branch Naming Conventions
# Create and switch to new branch
git checkout -b feature/new-ui
# Create branch without switching
git branch feature/new-ui
Switch Branches
Move between branches:
# Switch to existing branch
git checkout main
git checkout feature/new-ui
# Switch to previous branch
git checkout -
Commit or stash your changes before switching branches to avoid losing work.
Delete Branches
Remove branches that are no longer needed:
# Delete local branch (must not be current branch)
git branch -d feature/completed-feature
# Force delete unmerged branch
git branch -D feature/abandoned-feature
Remote Operations
Add Remote
Connect your local repository to a remote:
Add Remote
Multiple Remotes
# Add remote repository
git remote add origin https://github.com/username/repo.git
# Verify remote
git remote -v
Push Changes
Upload local commits to remote repository:
Commit Local Changes
Ensure all changes are committed locally
Select Push
Choose Git > Push from the menu
Select Branch
Choose the branch to push
Enter Credentials
Provide authentication if required
# Push current branch to origin
git push origin main
# Push and set upstream tracking
git push -u origin feature/new-feature
# Push all branches
git push --all origin
Pull Changes
Download and merge changes from remote:
# Pull from default remote/branch
git pull
# Pull from specific remote/branch
git pull origin main
# Pull and rebase instead of merge
git pull --rebase origin main
Pull operations fetch remote changes and automatically merge them into your current branch.
Fetch Updates
Download remote changes without merging:
# Fetch from all remotes
git fetch --all
# Fetch from specific remote
git fetch origin
# View fetched changes
git log HEAD..origin/main
Viewing History
Commit Log
View the commit history of your repository:
View History
Detailed History
# View recent commits
git log
# View compact history
git log --oneline
# View last N commits
git log -10
Commit Information Displayed:
Commit hash (full and short)
Author name and email
Commit date and time
Commit message
Changed files
File History
View the history of a specific file:
# View commits affecting a file
git log -- path/to/file.java
# View changes to file
git log -p -- path/to/file.java
Advanced Features
Discard Changes
Revert uncommitted changes:
# Discard changes to specific file
git checkout -- path/to/file.java
# Discard all changes
git checkout -- .
Discarding changes is permanent and cannot be undone!
Unstage Files
Remove files from staging area:
# Unstage specific file
git reset HEAD path/to/file.java
# Unstage all files
git reset HEAD .
.gitignore Support
The IDE respects .gitignore files:
Common .gitignore Entries
# Build outputs
build/
* .apk
* .aab
# IDE files
.idea/
.gradle/
local.properties
# Generated files
* .class
* .dex
* .log
Add sensitive files like API keys and credentials to .gitignore to prevent accidental commits.
Conflict Resolution
When merge conflicts occur:
Identify Conflicts
Git marks files with conflicts as “Conflicting”
Open Conflicted File
Edit the file to resolve conflict markers
Remove Conflict Markers
Delete <<<<<<<, =======, and >>>>>>> lines
Stage Resolved File
Add the resolved file to staging
Complete Merge
Commit the merge resolution
<<< <<< < HEAD
Your local changes
=======
Incoming changes from remote
>>>>>>> branch-name
Authentication
HTTPS Authentication
For HTTPS remotes:
Username : Your GitHub/GitLab username
Password : Personal Access Token (PAT)
GitHub: Generate tokens at Settings > Developer settings > Personal access tokensGitLab: Generate tokens at User Settings > Access Tokens
SSH Authentication
SSH support may be limited on Android. Use HTTPS with tokens for best compatibility.
Troubleshooting
Push Rejected
! [rejected] main - > main (non-fast-forward)
Solution:
# Pull remote changes first
git pull origin main
# Resolve any conflicts
# Push again
git push origin main
Authentication Failed
If authentication fails:
Verify username and password/token
Check token permissions (needs repo access)
Try generating a new access token
Ensure no typos in repository URL
Repository Not Found
Double-check:
Repository URL is correct
You have access to the repository
Repository exists and is not deleted
Network connection is stable
Large File Warnings
Git is not optimized for large binary files. Consider using Git LFS for files over 50MB.
Best Practices
Commit Frequently
Make small, focused commits
Commit working code, not broken code
Write descriptive commit messages
Branch Strategy
Use branches for features and fixes
Keep main/master branch stable
Delete merged branches
Before Pushing
Review your changes
Test your code
Pull latest changes
Resolve conflicts locally
Security
Never commit secrets or API keys
Use .gitignore for sensitive files
Review changes before committing