So, what is Git ?
If you are new, or fairly new to the software development world you would most likely already heard of Git.
Git is a tool used for source code management, it is a free and Open Source Version Control System that has branching capabilities to facilitate popular workflows, such as the feature branch workflow.
Git integration with Android Studio ?
Over the years Android Studio has been getting many updates, and its direct integration with Git has been getting better and better. Nowadays you can do almost everything you need to do in a simple way, straight from Android Studio, without having to use the command line or a 3rd party software.
Using Git straight from Android Studio
I will talk about most of the Git commands you will potentially need in your day to day as an Android Developer. For each command you will have: an explanation of the command, when to use it, and a video demonstration.
Note that I won't be going into a lot of detail about each particular Git command, however, you can see the official documentation for all of them right here for a more in depth look at them.
Cloning a project from a Git repository
Let's start by setting up our project.
As you can see, all we need to do is open Android Studio, select "Get from VCS", have Git, selected and paste the project URL. This will clone the project and enable you to start working on it.
Creating a new Branch
Now that we have our project opened in Android Studio we can start by creating a new branch to work on. This new branch will be based on the branch you have currently open, so make sure it is the correct one.
Generally, especially if your team is using one of the most common workflows, work is never done on the main branches (main, develop, etc..) instead you would always create a new branch to work on new features, bug fixes, or other types of tasks.
Doing a commit is the action of saving your changes into your local repository.
Deciding when we should commit our changes comes with experience and is often based on feeling. My recommendation is that you should be committing early and often. You can think of it like this: you have done something, let's save it, and you should create a commit. At the end you have the option to just Commit or to Commit and Push, in this particular example we will just Commit.
Alongside the commit we should be providing a commit message, keep it clear and concise.
Edit a Commit Message
Sometimes mistakes happen. We might have written something incorrectly in our commit message; let's see how to edit it.
Note: This is only in the instance where you haven't pushed the commit yet. Otherwise, it is a more complicated problem that we will not be exploring in this post as this deserves a more detailed and advanced explanation.
You should Push your changes into the remote repository when:
You are done with the feature you are working on and are ready to create a pull request, or you want to have your changes in the remote repository, perhaps in anticipation of sharing with other people or just to have your changes in a safe place other than your local machine.
Pull (Update project)
Doing a Pull will bring all the latest changes from the remote repositories.
You should do this when you want to update your local branch with changes that are already in the remote repository. It is advised to always do this before starting a new branch to make sure you are working with up-to-date code. Pull will only do this for the branch you select, while Update Project will do it for the whole project - even if it has multiple modules; including all the branches in your local repository.
Doing a Merge will allow you to integrate changes from other branches into your branch, or a branch of your choosing.
While doing a merge, Git will take care of integrating the changes into the branch you desire. However there is a chance of having “conflicts” that cannot be resolved automatically - and you will need to resolve them manually. Conflicts happen when the branch you are trying to merge already has changes in the same parts of the code, or the same lines.
You should be merging when you want to integrate work that was done in one branch into a different one.
In this example you see a merge that generated some conflicts and how we manually resolved them. The resolution might differ depending on the situation, it's not something that always has the same solution.
You now have the foundational knowledge to do most of the Git commands that you will need straight from the Android Studio, saving you time and the need to have other tools to handle Git interactions.
In the next post I will go into more advanced commands like Rebase and Cherry-Pick, in the meantime if you have any questions or doubts fell free to contact me