Skip to main content

Command Palette

Search for a command to run...

Git for Beginners: Complete Guide with Essential Commands

Learn Git from scratch with a unique problem-solving approach. Understand why each Git command exists, master the basic workflow without mugging up!

Updated
7 min read
Git for Beginners: Complete Guide with Essential Commands

Now that we know from the previous blog why Git exists, let's learn what Git actually is? The basic terminologies and the essential commands of Git with a problem solving approach.

What is Git? Git is a basic software which is generally referred to as a distributed version control system. Distributed means everybody can have their own version of the same file allowing them to make changes and a version control system simply means a software which tracks the changes made to a folder of files. This folder is generally referred to as repository.

What is GitHub? It is the hosting server for Git repositories (remote/cloud).

💭How was Git developed?

Linus Torvalds the creator of Linux, while working on Linux kernel, he and his team faced a licensing dispute over their previous proprietary version control system. So out of the necessity he developed the initial version of Git within 10 days and got back to his main project.

Now that we know what Git actually is and how was it developed let's go through the useful Git terminologies.

📖Git Terminologies:

Now let's dive into the commands which help us use Git.

🔧Understanding basic Git commands

The best way to remember Git commands is by understanding the need of those commands. So let's think about the thought process of Linus Torvalds sir while inventing Git. Note: Here we are just understanding the commands of Git, we will discuss the workflow later. To build a software similar to Git, we will first have to create a file in the same folder that will store the changes made in the file. Also let's make the file hidden as it is of no use to the user and accidental overwrite can be prevented.

Tedious process to do each time that too manually right? For this we will map a command that will do this for us. Let's name this command as "git init" as it initializes the Git file in our directory.

📍command for creating a file that will track the changes of the targeted folder-

git init

Now we have a file which tracks the changes for us, but how frequently will it record the changes? Linus sir decided that let the frequency of recording the changes be given to the developer. Since adding a definite time stamp would simply add performance burden on the system and the timeline would have got flooded with unnecessary records of updates.

So to ask Git to record the changes in our repository(folder), we assigned it a command as "git add .", as it adds the current changes in the folder as compared to the previous version.

📍Command for asking Git to record the changes in the targeted folder-

git add .
  • the period is the part of the command and means all the files in the current directory.

📍If a filename is written in the follow up with the "git add" command the changes in that specific file are tracked by Git.

git add hello_world.cpp

In this step, info related to the developer performing the change, the time of change and the changes made are all stored locally in the Git file.

Now since we have moved one step ahead of the initial step we should have some kind of accomplishment feel right? So let's name this stage as "The Staging Area". The files in this area are considered ready to be uploaded globally that is to GitHub.

📍To actually commit the files and the track of the changes to Git, we use the command-

git commit -m "this is the commit message"

The commit message is a note from the developer written in the present tense which informs about the updates made in the current version of the file.

📍To make remote accessibility and collaboration possible for our project so connect the local Git to remote GitHub using the "git remote add" command followed by the GitHub url-

git remote add <remote_name> <url>

📍Now finally to push the code to GitHub, we use the command-

git push -u origin main

Wonder what is this “-u”? It is a flag that sets upstream tracking from the local repository to the remote repository.

Good going right? Not always, what if we want to check the current state of the files in the working directory? Don't worry Linus sir has got your problem solved. Use the "git status" command to checkout the states of files.

📍To check the status of the files-

git status

The "git status" command also provides information about:

  • Files that have been modified but not committed (Modified files)

  • Files that are not being tracked by Git (Untracked files)

What if we want to see all the versions of the files that we have committed? Simply use the "git log" command and it will give you detailed info about the commits made.

📍To checkout the history of commits-

git log

Now that we know the commands let's discuss the workflow from scratch-

🧠Basic Developer Workflow using Git

📁Step-1: Creating a folder for the project

Start by creating a folder for the project:

mkdir gitTrial   
cd gitTrial

Remember, creating a (git)file that saves the changes made?

🔌Step-2: Initialise Git

git init
  • Creates a .git folder inside the gitTrial folder

  • Now the project is being tracked by Git

✍️Step-3: Create files

Create a file:

touch index.html

Add lines of code:

Hello Git

Now we need to ask Git to track this file right?

📦Step-4: Add files to Staging Area

git add index.html
  • The file is now added to the staging area.

Let’s take a little detour and see what Git status does?

🔍Step-5: Check the file status

git status

Now let's update the remote as discussed.

💾Step-6: Commit

git commit -m "Committing the first commit"

Setting up the remote url-

☁️Step-7:Connect to GitHub

Create a repository on GitHub.

Connect it-

git remote add origin <github_repo_url>

🚀Step-8:Push the code

git push -u origin main

Now this version will remain in the remote GitHub repo unless you exclusively delete it.

Some common Git Commands:

Note: No need to remember the commands, just go through them.

Git CommandReal-World MeaningBeginner Friendly (Demo + Short Info)
git initStart a projectgit init → Turns your folder into a Git project
git statusSee file changesgit status → Shows modified, new & staged files
git add .Choose filesgit add . → Adds all files to staging
git commitSave progressgit commit -m "login UI" → Saves a snapshot
git logSee historygit log → Lists all saved commits
git diffSee what changedgit diff → Shows exact code changes
git branchList branchesgit branch → Shows all available branches
git checkout loginSwitch workgit checkout login → Moves to login branch
git checkout -b uiNew featuregit checkout -b ui → Creates & switches branch
git merge uiCombine workgit merge ui → Adds UI changes into main
git stashSave work for latergit stash → Temporarily removes changes
git stash popRestore workgit stash pop → Brings back stashed code
git remote addConnect repogit remote add origin https://github.com/ronny/app.git → Links local project to GitHub
git pushUpload projectgit push origin main → Uploads code to GitHub
git pullDownload updatesgit pull → Gets latest code from GitHub
git cloneCopy repogit clone https://github.com/ronny/app.git → Downloads full project
git reset --hardDelete changesgit reset --hard → Removes all unsaved changes ⚠️

Conclusion:

Every software seems rocket science at first, but when logics of particular things are figured out even rocket science is not actually rocket science.

The best advice: No need to remember anything, just go with the logics and then practice it.