Git & GitHub for Android Developers | Git guide for Android Studio

Version Control System: Git and GitHub for Android Development with Android Studio

Before getting into Git let me ask a question . How do you backup your android project after completing whole project or just adding a core functionality. Here may be your two answers1. You don’t take any backup at all(very bad habit).2. You might convert project folder to zip file and save the file with added functionality name but is it scalable thing to do because this will cause you each time make a new zip file and renaming with new name and the most important it consume a lot of storage with this method.
Then what is best way to main the project history. here comes into play the Git.

Imagine you’re building a Lego castle. As you add pieces, it gets awesome! But what if you make a mistake? With regular Legos, you might have to break the whole thing apart.

Git is like a magic box for your code (like Legos for programmers!). It lets you:

  • Take snapshots: Every time you finish a cool part of your code, Git takes a picture (or snapshot) of it. This snapshot is called a “commit “.
  • Go back in time: Did you mess something up in your latest code? No problem! Git lets you rewind to an earlier snapshot (commit) and fix things from there.
  • Work with friends: Building a giant castle with a friend is more fun, right? Git lets multiple developers work on the same code at once. They can see each other’s changes and even combine their work.

Here’s a simple analogy:

  • Think of your code as the castle.
  • Each commit is like a completed section of the castle (e.g., the walls, the towers).
  • Git remembers all the snapshots (commits) so you can see how the castle evolved.
  • Multiple people can work on different parts of the castle (code) at the same time.

Git is a command line tool which maintain the code history of your project. It takes snapshot for your project at any given time when you tell it to do so. It tracks every change you make. Or say it is your projects Time Machine means with this tool you can travel back to the past of your project. Each time you make any changes in your codebase then save this change with a name to git(Naming will help you later to identify what you actually changed at that time in code. In Git language this naming thing is called as versioning of your project means with each new version you project is adding some cool features and it gets better. But keep in mind the that git will maintain code history of our project only in your local machine so to overcome this problem comes the GitHub which is a cloud storage of your project history which will remains till the end of world. So before learning how to integrate git in you android project lets know all key benefits of Git. 

  • Revert to previous versions: Made a mistake? No worries! Go back in time and restore a working version.
  • Collaborate effectively: Work with other developers on the same project, seeing each other’s contributions and merging them seamlessly.
  • Maintain a clean code history: Track the evolution of your codebase, understanding how features were built and changes were made.

GitHub: Your Code’s Home in the Cloud

GitHub is a web-based platform that acts as a central repository for your Git projects. It offers features like:

  • Public or private repositories: Choose to share your code publicly or keep it private for team collaboration.
  • Issue tracking: Manage bugs, feature requests, and tasks efficiently.
  • Code review: Collaborate on code improvements through pull requests and code discussions.
  • Version control visualization: See a clear history of commits and branches in your project.

Before using git or GitHub for your project lets make yourself comfortable with all the terminology related to VCS .
1. Repository: It the a new name for your project in world of git. Every project for which you want to enable version control system is a repository.
2. Commit: It is name of process in which we add the changes of your project into git or say when we commit our changes to git then it will take the snapshot of that particular change and keep that for history. Remember each git commit should have a name for it so that you have an idea on later stage that what has happened in this commit.
3. Untracked or UnVersioned: If you want that some files of your project should not be included in git like secret key files. then you can keep these files untracked. To do so you need to add name of these files in the .gitignore file which has automatically inserted into your project when you initialized git for your project.
4. Revert : If you made some mistake in your project in a new commit and you realized this after commiting the new code change then what. Don’t worry you can undo the fucked up code by reverting to the previous functional commit.
5. Checkout: In revert you simply go back to the last commit you have made but what if you want to go the stage of your project where your have made your first commit then you need to check out to that specific commit. 
6. Stash Changes: Suppose you are working on some new feature but you need to checkout to a previous commit or say previous version. then there are two options first is you can check out to that commit without saving these changes which is called forced checkout. In this case all your changes will be gone. Another better way is stash the changes and then checkout to a commit which is called Smart checkout. In this case you can later Unstash or get back these changes. Stash is a temporary history but commit is a permanent history.
7. Branches: In git all the commit you made are by default on master or main branch of your project. But you can make any number of branch and can make commit to that branch too. Generally we make branches when we want make some new features in project so we add new code in new branch and try to test the project if its working fine. If this new features works fine then you will merge(Next Definition) this change to the main branch.
8. Merge: It means to combine or join together the code changes into your main codebase. We generally merge two branches into each other. You will learn later that this is very complicated process.
9. Cloning: When your project is hosted on GitHub then one new employee will clone the project from GitHub to his machine. Cloning means making copy of project. 
10. Push changes: When we make some changes in code then we commit this change but this will keep our  changes only on our local machine.SO to update our cloud repository which is in GitHub we need to push these changes to GitHub repository.
11. Pull Changes: Suppose one team of your company make some changes in the project. SO to get those changes you need to pull these changes to your machine. Pull is similar to cloning the project but in this case we only get the updated changes.
12. Collaboration: It is the process of working many individual on same project. They all can make some changes and make you project better. Collaboration means working together and git makes it more easy.
13. Remote: A common repository on GitHub that all team member use to exchange their changes.
14. Fork: A copy of a repository on GitHub owned by a different user.
15. Head: Representing your current working directory, the HEAD pointer can be moved to different branches, tags, or commits when using git checkout.

Android Studio and Git: A Perfect Match

Android Studio integrates beautifully with Git, making version control a breeze:

1. Setting Up Git:

  • If you don’t have Git installed on your system, download it from https://git-scm.com/downloads.
  • Within Android Studio, go to VCS > Enable Version Control Integration.
  • Choose Git as the version control system.

2. Creating a Local Git Repository:

  • Open an existing project or create a new one.
  • Go to VCS > Git > Add. This stages all your project files for version control.
  • Enter a meaningful commit message and click “Commit.” This creates a snapshot of your current codebase.

3. Connecting with GitHub:

  • In Android Studio, go to VCS > Git > Push.
  • Enter your GitHub credentials and create a new remote repository (or link to an existing one) on GitHub.
  • Push your local commits to the remote repository on GitHub.

4. Branching and Merging:

  • Branches allow you to work on features or bug fixes without affecting the main codebase (master branch).
  • Use VCS > Git > Branch to create a new branch.
  • Make your changes and commit them to the new branch.
  • Once ready, merge your branch back into the master branch using VCS > Git > Merge.

Collaboration Magic:

  • Team members can clone the repository from GitHub to their local machines.
  • They can create their own branches, make changes, and push them to the remote repository.
  • Pull requests allow you to review changes before merging them into the master branch, ensuring code quality and collaboration.

Essential Git commands you’ll frequently use in the Android Studio terminal:

Initializing a Repository:

  • git init: Creates a new Git repository in your current directory, marking it for version control.

Tracking Changes:

  • git add <file>: Adds a specific file to the staging area, preparing it for the next commit.
  • git add .: Adds all modified and untracked files in the current directory to the staging area.
  • git status: Shows the status of your working directory, indicating which files are modified, staged, or untracked.

Committing Changes:

  • git commit -m "<message>": Captures the current state of the staged files as a commit with a descriptive message.

Branching and Merging:

  • git branch <branch_name>: Creates a new branch to diverge from the current branch (usually master).
  • git checkout <branch_name>: Switches to a different branch.
  • git merge <branch_name>: Combines the changes from another branch into the current branch (be cautious of conflicts).

Remote Repositories (GitHub):

  • git clone <url>: Downloads a Git repository from a remote location (like GitHub) to your local machine.
  • git remote add <name> <url>: Adds a remote repository (e.g., GitHub) to your local project, giving it a nickname for reference.
  • git push <remote_name> <branch_name>: Pushes your local commits to the specified branch on the remote repository.
  • git pull <remote_name> <branch_name>: Fetches updates from the remote repository and merges them into your local branch (can involve conflict resolution).

Undoing Changes:

  • git checkout -- <file>: Discards changes made to a specific file in the working directory.
  • git reset HEAD~<n>: Moves the HEAD pointer back n commits, effectively undoing the last n commits (use with caution).

Viewing History:

  • git log: Shows a chronological list of commits made in the repository.
  • git log --oneline: Provides a more concise view of the commit history.

To get more Command: These commands provide a solid foundation for version control with Git in Android Studio. There are many more commands available, and it’s recommended to go on the official Git documentation (
https://git-scm.com/doc) for complete details .

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top