An Introduction to Git for Beginners

Chaya Thilakumara
Chaya Thilakumara
Published in
5 min readMar 15, 2021

There are so many tutorials with git commands which anyone can copy & paste, but they really don’t explain how git works, how can you use git commands, and what git can do for you.

But this tutorial will help you to understand the git & basic commands you’ll use frequently.

Git and GitHub are not the same...

Git is the most commonly used version control system (VCS). Git tracks the changes you made to the files, so you have a record of what changes you made, and you can revert to specific versions whenever you need it.

GitHub is a website that manages projects that use git. You do not need GitHub to use git, but you cannot use GitHub without using git. There are many other alternatives to GitHub, such as GitLab, BitBucket etc.

Git is software that works locally, your files and the revision history are stored on your computer. You can also use Github online hosts to store a copy of the files and their revision history.

Git also makes collaboration easier with multiple people, where you can upload your changes and download changes from others from a centrally located place.

This article mainly focuses on the basic SHELL commands & GitHub as the hosting service for Git.

Basic GIT Workflow

Git workflow consists of

  • A working local directory: It is the local directory your working on
  • A Staging area: It is the area where you add changes you make to the working directory
  • A Repository: It is the place where Git permanently stores the changes

Create a local git repository

When you want to create a new project on your computer using git, the first thing you do is create a new repository.

To create the repo, open up a terminal and move to where you want to place the project on your computer using the cd (change directory) command.

For example, if you want to create your project folder on your desktop, you will do something like this:

Create a local git repository

cd [directory-name] — moves to the given directory name

mkdir [directory-name] — makes a new directory with the given name

 git init

This will create a hidden .git folder in your project folder on your computer. .git folder is the repository where it stores all the changes you make to any files within the project folder.

The Noob Guide to GIT [Part 1] | LaptrinhX
Create a hidden folder ‘.git’

Clone an existing repo

git clone <your-copied-github-url>

This will download the repository from GitHub to your computer with the latest snapshot of the repo to your project folder.

To do this we need to navigate to the repo on Github and find a green button that says “clone or download”.

Clone your repo by copying this URL

Once you click that button you’ll see a dropdown that provided you with a URL to copy. (Make sure to select the HTTPs format)

Check the current status of your project

git status

You can check your git status anytime you need to get additional information depending on the current status of your project, such as which files have recently been modified.

Create a new branch

A branch creates a copy of the main repo and creates a branch that lets you make changes without changing the main branch.

git branch <new branch name>
Create a new branch

Creating a new branch allows you to work on a separate copy of your code without affecting the main branch. When you first create a branch, a copy of your main branch is created with a new name. You can then modify the code in a new branch. Once your new feature code is stable, then you can merge those changes into the main branch.

Check out an existing branch

git checkout <existing-branch-name>

You can use git checkout when you want to resume from an existing checkpoint. There all your files will be reset to whatever state they were in on that particular branch.

If you want to keep any changes in your working directory without committing them locally, you can usegit stash .

Check out to a new branch

Flag -b the flag is the shortcut to create a new branch.

git checkout -b <new-branch-name>

Check the differences between checkpoints

git diff <branch name> <other branchname>

Before committing your work, it is better to view the changes you have made, using git diff the command.

Files which are used prefixed with "-" and colored red Files which looks like now prefixed with "+" and colored green

Stage your changes to prepare for committing them

git add <files>

This command is useful for controlling exactly what you commit and will mark any changes you have made as “Staged” or “ready to be committed”. Even if you have changed the same files as before, those file changes will not automatically be staged.

If you need to know exactly what changes you made, you can just type git statusand see “Changes to be committed” in green and “Changes not staged for commit” in red.

If you want to add every file you change, just type git add --all .

Commit your staged changes

git commit

This will ask you to type in a commit message. The commit message helps to understand what was changed.

The -m the flag is used to write a message. For example:

git commit -m “Add a new feature”

Push your branch to upload it somewhere else

git push origin <branch-name>

This will upload the branch to the remote named origin .

After pushing your changes, others can pull your branch to view your commits.

Fetch the latest info about a repo

git fetch

This will download the latest info about the repo from origin .

Merge in changes

Once you’re done adding the new feature to your branch, you’ll want to merge it into the main branch, so that your main has all of the features.

git merge <other-branch-name>

This will take all commits on the other-branch-name branch and integrate them into your own current branch.

You can use the pull command to both fetch and merge all in one step.

git pull origin origin/master
Git Workflow Diagram

There’s a lot more you need to understand in order to become a git expert, but for now, you have some idea how it works. Thank you, and stay tuned for the second part.

Till then happy testing!!!

--

--

Chaya Thilakumara
Chaya Thilakumara

Pursue your passion, and everything else will fall into place.