How To Use Git ? (Beginner)

Arshad Suraj
6 min readMay 15, 2021

--

Image from — Google search

What is Git?

According to Git’s official documentation, “Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency”.

It is essential for a developer team to keep track of their codes. However, manually tracking all of the changes made by each team member over time is extremely difficult. Git, on the other hand, will help you keep track of which changes were made, by whom, and when, as well as giving sufficient functionality. If you end up with a bug in your code, Git becomes even more important!

Is Git & Github are same?

GitHub is a service that lets you host your Git projects on a remote server (or in other words, in the cloud). It’s important to note that GitHub and Git are not same. GitHub is nothing more than a hosting site. There are number of companies such as Bitbucket and GitLab etc which provides hosting services similar to GitHub.

Let’s get started on learning how to use git step-by-step.

Download Git

Click here to get details on how to install Git in your operating systems. Use the following command in the command prompt to see if Git is installed correctly.

git --version

Create a Local repository

1st create a folder (a workspace) for your project. Navigate to the folder and enter below command to create a local repositary.

git init

What is Local repository? — The local repository is the .git folder inside our project folder. It will track all the changes made to the files in our project and record that history over time.

Add some files

create a file (Ex: Helloworld.java) within the folder and add some codes.

Staging

Use the following command for add the file to staging area

git add <file name>
Ex:
git add helloworld.java
//In case if you have to add multiple files
git add <file name> <file name> <file name>
Ex:
git add helloworld.java Employee.java Customer.java
// In case if you want to add all the files and folders inside your project folder
git add .

Git will keep track of our code only if the file is in staging area. Even if the file is located inside the project folder, git will not keep track of it if it is not added to the staging area. Furthermore, only the changes made to files in the staging area will be committed when we commit.

Committing

Use the following command to commit the file

git commit -m <commit message>
ex:
git commit -m "Initial Commit"

Note: If we modified a file after committing it or when it’s in the staging area, the file’s status changes to “modified” and it’s no longer in the staging area. To commit it, we must first add them to the staging area again, and then only we can commit it.

To add files to the staging area and commit the file in one step, use the following code

git commit -a -m <commit message>
Ex:
git commit -a -m "file is modified"

Status

use the following command to view status of each file in our project.

git status

Log

use the following code to view all the commits which have been done up until now.

git log

Each commit’s author, commit Id, commit date, and commit message will be listed in the log.

Branches

A branch is a lightweight, movable pointer to the most recent commit in a project. Git allows for concurrent development of the same project by keeping several branches that are entirely separate from each other. A modification in one branch has no effect on the other branches. This enables developers to work on several features at once.

Branches — Picture by-google

Create a new branch

Use the following code to create a branch

git branch <branch name>
Ex:
git branch test

This command will create a new branch with current commit

Switch to another branch

Use the following command to switch to the new branch (or another branch).

git checkout <branch name>
Ex:
git checkout test

You can use “git branch” command to list down all the branches in local repository

Merging

Using several branches, multiple developers are working on different features. As a result, those all branches should be merged into one branch at the end of the day, since all of the features belong to the same project. The changes are usually merged into the main/master branch.

Please keep in mind that in git we are merging another branch into the current branch. For Ex: In order to merge the “test” branch into the “master” branch, we must first go to the “master” branch and then merge “test” branch to “master”.

For Ex: use the following code to merge “test” branch with “master” branch

git checkout master
git merge test
Merge — image by-google

When a merge is performed in a real project, however, there can be conflicts. If those two branches have two different codes for the same line of code, git can’t determine which should be kept and which should be deleted. This is called merge conflict. When merge conflicts occur, it keeps codes from both branches. merge conflicts must be resolved manually by the developer.

The Remote Git Repository

Each developer may work in his or her own local repository, but the code will finally be pushed to a remote repository. Once the code is pushed to the remote repository, other developers can see and modify that code.

In this article, we will be using GitHub as the remote repository.

First Sign up into GitHub account, then create a repository, then copy the remote repository URL as shown below

Remote Repository URL

Use the following command to point your local repository to the remote repository

git remote add origin <Remote repository url>
Ex:
git remote add origin https://github.com/Mohamed-Arshad/Krish-LP-Training

Push

use the following command in order to push all the code from the local repository into the remote repository

git push -u origin <Local Branch>
Ex:
git push -u origin master
//If you want to push all of your local repository's branches to a remote repository
git push -all

Pull

“git pull” is used to pull the latest changes from the remote repository into your local repository. below is the command used for pull

git pull origin <remote branch>
Ex:
git pull origin master

Clone

The command “git clone” is used to clone an existing remote repository into your local repository

git clone <Remote Repositary URL>
ex:
git clone https://github.com/Mohamed-Arshad/Krish-LP-Training

More on Git

Tags

Tagging allows developers to identify important checkpoints in the progress of their projects. For example, Versions of software may be tagged. (For instance, v1.3.2) It enables you to assign a commit a unique name (tag).

Stash

Git has an area called “stash” where you can temporarily store a snapshot of your changes without committing them to the repository. It’s separate from the working directory, the staging area, or the repository.

Keep Learning ❤️

--

--

Arshad Suraj
Arshad Suraj

Written by Arshad Suraj

Associate Software Engineer at Virtusa

No responses yet