loading...
Cover image for 13 Important Git Commands Every Android Programmer Should Know

13 Important Git Commands Every Android Programmer Should Know

wamiikechukwu profile image Wami Ikechukwu ・4 min read

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.

Prerequisite

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.

SETUP & INIT GIT ON YOUR PROJECT

Configuring user information, initializing and cloning repositories.

Example 1:
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.

`git Init` 

This initialize an existing project directory as a Git repository, that means all changes to your code are stored here.

Example 2:
To clone a project from a repository in github.

`git clone [repository url]`

This command gets/retrieve an entire repository from github via URL

STAGE & SNAPSHOT IN GIT

Working with snapshots and the Git staging area

Example 3:

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.

`git status`

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.

Example 4:

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 *).

Example 5:

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.

GIT BRANCHING & MERGING

Example 6:

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.

Example 7:

If we need to know which of the branches you are currently on in Android Studio, use this command.

`git branch`

This command will list your branches. a * will appear next to the currently active branch.

Example 8:

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.

Example 9:

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.

Example 10:

Want to see what is saved or any changes, this will do.

`git log`

This show all commits (changes) in the current branch’s history.

SHARE & UPDATE IN GIT

Retrieving updates from another repository and updating local repository.

Example 11:
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.

Example 12:

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”.

Example 13:
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.

Posted on by:

wamiikechukwu profile

Wami Ikechukwu

@wamiikechukwu

A dedicated Android Developer, that is aimed at building successful products, that will make the world a better place.

Discussion

markdown guide
 

these are general commands, why you specified for android. did I miss something?

 

Right? I was wondering the same thing.

 

My audiences were Android Developers.

 

Actually you are right, but as an Android Developer. We tend not to use git, so basically my audiences were Android Developers.