Git Cheatsheet for Android Development with Android Studio Terminal

Let’s dive into some detailed examples for common scenarios and setups in Android development with Git and Android Studio terminal:

1. Setting Up a New Android Project with Git

Let’s say you’re starting a new Android project and you want to set up a Git repository from the beginning.

Steps:

  1. Initialize the Git repository: Inside your Android project folder, run:

    git init
    
  2. Create a .gitignore file: Android projects usually include .gitignore files to prevent certain files from being tracked, like build files and IDE configurations. Here’s a basic .gitignore for Android:

    # Android
    .gradle/
    .idea/
    *.iml
    build/
    *.apk
    *.log
    local.properties
    

    You can create this file manually or use GitHub’s or GitLab’s default Android .gitignore template.

  3. Add all files to the staging area:

    git add .
    
  4. Commit the initial project setup:

    git commit -m "Initial commit of Android project"
    
  5. Set the remote repository: First, create a repository on GitHub or GitLab, and then add the remote URL to your project:

    git remote add origin <repository_url>
    
  6. Push the code to the remote repository:

    git push -u origin master
    

2. Working with Branches in Android Studio

Let’s walk through the process of creating a new branch for a feature and pushing it to Git.

Steps:

  1. Create a new feature branch: Use this command to create and switch to a new branch:

    git checkout -b feature/user-login
    
  2. Make your changes in Android Studio: After implementing the feature (e.g., creating a user login screen), add the files to the staging area:

    git add .
    
  3. Commit the changes:

    git commit -m "Implemented user login screen"
    
  4. Push the branch to the remote repository:

    git push origin feature/user-login
    
  5. Create a Pull Request (PR) on GitHub/GitLab: Once the branch is pushed, you can create a PR from the GitHub/GitLab interface to merge it into the main or develop branch.

3. Merging a Branch into main Branch

After your feature branch is complete and has been tested, it’s time to merge it into the main branch.

Steps:

  1. Switch to the main branch:

    git checkout main
    
  2. Pull the latest changes from the remote main branch:

    git pull origin main
  3. Merge the feature branch into main:

    git merge feature/user-login
    
  4. Resolve any merge conflicts (if any), and then commit the merge:

    git commit -m "Merged feature/user-login into main"
    
  5. Push the changes to the remote repository:

    git push origin main
    

4. Reverting or Undoing Changes

If you made a mistake or want to discard changes, you can use git reset or git checkout:

Example 1: Undo the last commit (keep changes in working directory):

git reset --soft HEAD~1

Example 2: Undo changes in a specific file:

git checkout -- path/to/file

Example 3: Undo staged changes:

git reset path/to/file

5. Working with Git in Android Studio Terminal

You can also use Android Studio’s integrated terminal to run these commands, which makes it easier to work with both Android-specific tasks and Git commands without leaving the IDE.

Example 1: Building and Running Your Android Project Using Gradle

  1. Clean your project:

    ./gradlew clean   # On Unix-based systems
    gradlew clean     # On Windows
    
  2. Build the APK:

    ./gradlew assembleDebug
    
  3. Install and run the app on a connected device or emulator:

    ./gradlew installDebug
    
  4. Run unit tests:

    ./gradlew testDebugUnitTest
    

Example 2: Checking Gradle Dependencies

  1. List all dependencies in your project:
    ./gradlew dependencies

Example 3: Linting Your Android Project for Issues

  1. Run lint to check for code quality and possible issues:
    ./gradlew lint
    

Example 4: Handling Build Failures

When a build fails, you can view detailed logs in Android Studio. You can also use the terminal to examine issues:

./gradlew build --stacktrace

This should cover most common Git workflows and using Android Studio’s terminal for building and managing projects. Let me know if you want to explore any specific command or setup in more detail!

📢 Feedback: Did you find this article helpful? Let me know your thoughts or suggestions for improvements! 😊 please leave a comment below. I’d love to hear from you! 👇

Happy coding! 💻✨


0 comments:

Post a Comment