I remember vividly well, when I lost the source code to one of my android apps on google play store due to my pc hard disk crashing.
It was a painful incident that could happen to any android developer.
All that happened because I didn’t get to use a Version Control System.
In this article, I will be highlighting some important Git commands every android programmer should know.
When writing this article, I presume that my readers have a little knowledge on the following:
- Basic Knowledge of how to use the terminal in Android Studio to type commands.
- Git installed on their local computers.
- A GitHub account.
- How to integrate Git/push their code from Android Studio.
NOTE: Throughout in this article, we will be writing Git command on the terminal in Android Studio.
Let’s assume you have a project on Android Studio and you wish to have it as repository path for your local changes. You run the command below on the android studio terminal.
This initialize an existing project directory as a Git repository, that means all changes to your code are stored here.
To clone a project from a repository in github.
`git clone [repository url]`
This command gets/retrieve an entire repository from github via URL
When you aren’t sure of the changes you have in your code that haven’t been stage, that is, saved to Git, you can use this command to check that.
When you run this command. It shows modified files in the working directory, that is, files that are ready to be staged for your next commit.
Now you have seen the files that needs to be committed, using this command will add them to the staging area.
`git add [file] / git add *`
With this command (
git add [file]), you can add a single file by typing the name of the file or you can add every file using this command (
git add *).
All files are at the staging area, ready to be saved into the Git repository.
`git commit -m [descriptive message]`
What this Git command does is to commit your staged content, meaning save your code/file with a description.
When you want to have two separate versions of your code. You need to have two Git branches, we use this command to achieve that.
`git branch [branch-name]`
This Git command create a new branch using the “branch name” you specify.
If we need to know which of the branches you are currently on in Android Studio, use this command.
This command will list your branches. a * will appear next to the currently active branch.
You want to move to one of your branches and see what has change, use this command.
`git checkout [Branch Name]`
This command switch to another branch and check it out into your working directory.
If you want to merge two code-base together into one. This Git command will do the trick.
`git merge [branch]`
This Git command, merges the specified branch’s history into the current one.
Want to see what is saved or any changes, this will do.
This show all commits (changes) in the current branch’s history.
There are situations, where you might want to connect your code to a remote repository on github. Though there might be various ways to achieve that using Android studio. I prefer using the Git command on the Android Studio terminal.
`git remote add Remote [url]`
This connect your code from your local computer to the github url provided. All changes to the code on your local computer, when push will appear on the github repository. Keep in the mind that the url must be a valid repository in github.
When you are done coding and need to save the code to github, use this command.
`git push –u [Remote branch]`
This transmit all change from the local branch commits to the remote repository branch, usually github. The remote branch most times have the name “origin master”.
Let’s try a different scenario. You have codes on your Version Control System such as github and you want this code to be in your local computer. Using this Git command, you can achieve just that.
`git pull origin master`
When this command is run on the terminal, it try to fetch recent commits from the tracking remote branch.
In summary, we have seen the 13 Important Git Command Every Android Developer Should Know.
Though there are more git commands than the ones I have mentioned here. I believe these are the essentials that an Android Developer should be using on a day to day basics.
Improving your Git/github skills is very important in becoming a software Engineer and shouldn’t be neglected.