DEV Community

Husnain Mustafa
Husnain Mustafa

Posted on

Git Basic Commands

Today, we will be learning about the Git Commands which are required when starting to work as a team. We will start from basics.

What is Git

First of all, let me tell you what is Git and why we need it as a software engineer.
If you have been doing some software developing, you might have faced a situation where you need to keep multiple backups of your project, each backup having some is newer version of previous, like, Backup_v1, Backup_v2. I used to do the very same when I started learning coding. But this kind of work is really unorganized and annoying. Then comes git, a tool which keep track of your code history so if at any time you wanna go back to previous versions, you can, with a single command. Other than keeping track of code, git is really useful when working on the project as a team.

Now, its our time to learn some git commands, and we will do it by going through a story. This will make it easy to understand.

The Story

Let's say Bob is a fresh Software Engineer who is hired by the company The Softee. The Softee is working on the project called Project X. Bob is given the access of the repository of Project X. Now Bob will have to download all the code of the Project X. So how will he do it?


Git Clone Command

Bob will download the repository by copying the repo and clone it in the terminal.
git clone <repo_link>
This will download all the code of the main branch to his PC.


Back to story, Now Bob got all the code. Bob has been given a task, Bug Task, to remove a small bug. So rather than starting working on the task, first Bob need to make a separate branch. But why? This is because if Bob does not code well, i.e, not having good quality code, or introducing another bug, or anything else that is not required, it will not affect the main branch. Separate branch keeps the Bob's code separate, and when everything is done by Bob's side, his code will be reviewed by the team and if everything is fine, then Bob's code will be merged to main branch. So to make a branch:


Making Branch in git

Bob will make a branch by:
git branch bugtask
This will make a branch but Bob is still on main branch, so to enter his newly made branch, he can use:
git checkout bugtask

To make a branch and enter in a single line, following command is used:
git checkout -b bugtask

He can also verify on which branch he is by:
git branch
It will list all the branched, and asterik, *bugtask, means he is on bugtask branch.


Now Bob is in his branch where he can start working on the bug. When he is done with removing bug, he will need to commit the branch. Committing in git means making a tracking points, i.e a version of backup. And it is necessary to commit before pushing the code. Pushing code means storing code on the remote repository, meaning it will be saved online on github. So even if code gets deleted from your PC, code will be available online. So Bob can make a commit by:


Committing changes in git

Before committing, Bob need to stage the files he want to commit, i.e he need to tell git that these are the files which are needed to be saved as a backup. So most of the times we need to save all the files, so we stage all files by:
git add .

If there is some specific file, we can:
git add <filename>

After staging files, he need to commit by:
git commit -m '<commit message>'

Commit message is a brief description about the commit. This could be whatever you want but its better to have some meaningful description so that at later, anyone can see what was done in this commit. Mostly we use Conventional Commits, a standard way to write a message of commit.


Now committing is done. Bob need to push the code, i.e send the code to remote repo so it gets saved online and everyone in the team can see it. So he can push by:


Pushing Code

He can push the code by:
git push -u <repo_name> <remote_branch_name>

-u is optional, it sets the upstream for the branch, i.e if you use -u, then it will link your local branch to your remote branch. So later you would not need to write the whole above command, rather just use:
git push
and git will understand that you are pushing your branch, bugtask to your repo. Git push will push the branch on which you are currently on.


Now code is pushed, and team can easily review Bob's code and if it is up to the mark, team will merge Bob's code to main branch, i.e add Bob's code to main branch.

Let's say Bob's pair programmer, Alex, also working on the same branch, bugtask, and he pushed some code too. So Bob can get the latest changes to the branch by:


Pulling Changes

git pull --set-upstream <remote_repo> <remote_branch_name>

--set-upstream is same as -u when pushing the code.
Both of these commands link your local branch to remote branch. So if you typed once -u or --set-upstream, it will link and then you can pull by simply:
git pull
and push by:
git push
while being in required branch, i.e, bugtask.


Now let's suppose another scenario where Bob is half-way between solving the bug that was assigned to him. Now he want to see the code of another branch, let's say branch-x, where he can get some help on how to remove a bug. Now Bob need to save the whatever half changes he did for removing the bug. So he can stash the changes, Stashing means saving the changes in the local PC so one can get those changes later. So Bob can stash his changes by:


Stashing Changes

git stash .
to stash all files that are changed.

git stash <filename>
to stash any specific file.


Now Bob can move around any branch and see all code he want to help him out by:


Changing Branch

git branch <branch_name>

In this case, as Bob want to see branch branch-x:
git branch branch-x

When he got the help, he can switch back to his branch by:
git branch bugtask


But now he wont have those changes that he did to remove bug. These changes are in stash so he can get back all the stashed changes by:


Getting Changed back from stash

He can get back the stashed changes by:
git stash pop
and all the changes he stashed in the most recent stash will be recovered in working directory.


I guess that is enough for this post. Soon we will be learning some advanced git commands which are really useful in the life of software engineer.

Hope it helped you out.
Feel free to ask anything.

Top comments (0)