Sitemap
Rajesh Kumaran

Rajesh Kumaran

A passionate software developer who loves to solve challenging problems

Git Basics for Beginners

6 min readJul 21, 2022

Hello everyone! 👋

One word that every developer encounters at some point in their career is Git. So, I thought — why not share some of the basic Git concepts and commands that can help beginners get started with confidence?

WHAT IS GIT ?

Git is an open-source version control system that helps you track changes in your codebase and collaborate effectively with other developers.

In simple terms:

Imagine you create a new project with zero lines of code.

  • You write 10 lines of code and commit them.
  • Another developer adds 10 more lines and commits their changes.

Git allows you to:

  • Track who made what changes
  • View the complete history of the project
  • Go back to earlier versions if needed

WHAT IS VERSION CONTROL SYSTEM ?

A Version Control System (VCS) helps software development teams manage source code over time. It keeps track of every change made by each collaborator and maintains a complete history of the project.

If something goes wrong:

  • You can compare changes between versions
  • Identify who made the change
  • Roll back to a stable version easily

To learn more about VCS, check out the official documentation:

👉 https://www.atlassian.com/git/tutorials/what-is-version-control

Difference between GIT and Github

GIT is a version control system whereas Github is a cloud-based service which manages the git repositories.

Press enter or click to view image in full size

Types of files

Try to have a basic idea of these file types, you will get to know more once you scroll down.

1. Modified files

These are the files which are already existing in repository ,but you have made some changes to it and not yet staged (not yet done -> git add ).

Press enter or click to view image in full size

In the above image, firstFile.txt is a file which was created on my previous commit , now I have done some changes so it becomes a modified file.

2. Untracked files

These are the files which are newly created but not yet staged (not yet done -> git add ).

Press enter or click to view image in full size

In the above image, untrackedfile.txt is a file which is newly created but not yet staged, so it becomes untracked files.

3. Staged files

These are the files which are staged and ready to be committed.

Press enter or click to view image in full size

GIT Commands with examples

1.git init
This command is to initialise a git repository.

Press enter or click to view image in full size

Created a project folder named gettingStartedWithGit and initialised with git in the above image.

2. git clone

This command is to download the repository from the remote.

Usage : git clone <clone-url>

Example : Assume you have a repository in github named CheckListApp.You can download that by git clone <clone-url> .

Clicking on that Clone or download button gives you a clone url.

Press enter or click to view image in full size

3. git add

This command is to stage all your files.

Usage: git add .

Press enter or click to view image in full size

4. git status

This command is to see the changes made in your repository like staged changes, untracked files.(ref to the above image)

Usage: git status

5. git commit

This command is to commit your changes and now your changes are ready to push to the remote so that other collaborators can see your work.

Usage: git commit -m “some useful message about the commit”

Press enter or click to view image in full size

6. git push

Get Rajesh Kumaran’s stories in your inbox

Join Medium for free to get updates from this writer.

This command is to push your changes to the remote repository.

Usage: git push origin <branch-name>

Press enter or click to view image in full size

Note : If your don’t have remote repository , create one repository(a.k.a project) in the github

Press enter or click to view image in full size

Click the Clone or download button and copy the repository url which will be shown.

git remote add origin <paste the url which is copied above>
The above command helps to connect to remote repository ,now you can do

git push origin <branch-name> to push your changes to the remote repository.

7. git pull

This command is to pull your latest changes from the remote to your local.

Usage : git pull origin master

Press enter or click to view image in full size

8. git log

This command is to see the history of changes made by other collaborators who are working on the same project.

Usage : git log

Press enter or click to view image in full size

9. git branch

This command is to see the list of branches and your current working branch.

Usage : git branch

Press enter or click to view image in full size

How to create a new branch (2 ways)

#1. git branch <branch-name>

This command creates a new branch from where you have created (source branch) and it won’t take you to the new branch.

Press enter or click to view image in full size
Press enter or click to view image in full size

#2. git checkout -b <branch-name>

This command also creates a new branch from where you have created (source branch) and it will checkout to your newly created branch.

Press enter or click to view image in full size

10. git checkout

This command is to switch between branches.

Usage : git checkout <branch to go>

Press enter or click to view image in full size

In the above image , switching from new-branch-using-checkout branch to master branch.

11. git merge

This command to merge your changes from one branch to another.

Usage : git merge <branch to be merge> (switch to your branch where the changes has to be merged with)

Press enter or click to view image in full size

In the above image,merging the changes from new-branch branch to master branch.

12. git stash

This command is to save your working progress which is not to be committed immediately.

Usage :

1. git stash -u

Press enter or click to view image in full size

This saves your changes and -u is to stash your untracked files (refer to types of files) as well.

2. git stash list

Press enter or click to view image in full size

This command shows the list of stash changes .

3. git stash pop

Press enter or click to view image in full size

This command is to pop your latest changes to commit.

To apply the specific stash changes , use this git stash apply stash@{n}

13. git fetch

This command is to fetch the remote data like branches,tags etc..but it does not affect the local branch.

Usage : git fetch origin <branchname>:<samebranchname>

Press enter or click to view image in full size

In the above image,trying to fetch the fetch-example ,but it doesn’t affect the local working changes.

Thanks for reading this. Hope it helps you. Share your thoughts in the comments below !!!

--

--

Rajesh Kumaran
Rajesh Kumaran

Written by Rajesh Kumaran

A passionate software developer who loves to solve challenging problems