Skip to main content

Command Palette

Search for a command to run...

Git for Beginners

Basics and Essential Commands

Updated
•4 min read•View as Markdown
Git for Beginners

1. What is Git ..?

Git is a distributed version control system. lets see who..?

Scenario: You and your friends are building a project together

Imagine you are working on a college project with 3 friends

Problem WITHOUT Git :

  • One person edits the file and sends it on WhatsApp

  • Another edits the same file and overwrites changes

  • You don’t know who changed what

  • You can’t go back if something breaks.

Solution :

Now imagine you have a software tool on your system that works like a smart tracker.

  • It records what was added , removed .

  • It keeps a history of every change

  • You can go back to older versions

This software is called Git.

Distributed means:

Every person has a full copy of the project and its history on their own computer and you don’t need the internet all the time to use Git

Git tracks changes in your files the and maintains a smart record of your project’s entire history, making teamwork safe and organized.

2. Why Git is Used ..?

  • track changes made to source code.

  • maintaining version history of a project.

  • branching and merging for parallel development.

  • prevent data loss by safely storing code versions.

3. Core concept of git and terminologies :

TermSimple ExplanationExample :
RepositoryA project folder that Git tracks and manages.Your project folder like student-portal. After running git init, it becomes a Git repository.
CommitA saved snapshot of your code with a message describing the change.Similar to clicking Save in a document. Example: “Added login page”.
BranchA separate line of development used to work on features safely.main branch has stable code. login-feature branch is used to develop and test the login system.
HEADA pointer that shows your current working position in Git.HEAD usually points to the latest commit of the current branch.

4. Common Git Commands:

SyntaxWhat it Does (Simple Words)When to Use
git initStarts Git tracking in the current folder by creating a .git directoryWhen starting a new project
git statusShows the current state of files (new, modified, staged)Before adding or committing files
git add file_nameAdds a specific file to the staging areaWhen you want to save selected files
git add .Adds all changed files to the staging areaWhen all changes are ready to save
git commit -m "message"Saves a snapshot of staged files with a messageAfter git add
git logShows complete commit historyTo see previous versions
git log --onelineShows short and clean commit historyFor quick review
git diffShows changes made but not stagedBefore adding files
git diff --stagedShows changes staged for commitBefore committing
git branchLists all branchesTo check current branches
git checkout branch_nameSwitches to another branchTo work on different features
git checkout -b new_branchCreates and switches to a new branchTo start a new feature
git remote -vShows connected remote repositoriesWhen using GitHub
git push origin branch_nameUploads commits to remote repositoryTo share code
git pull origin branch_nameDownloads latest code from remoteTo sync changes
git clone repo_urlCopies a remote repository to your systemWhen starting from GitHub

Basic developer workflow using Git from scratch :

Git, GitHub, & Workflow Fundamentals - DEV Community

Git Workflow (Simple Explanation)

The working directory is where you create and edit files.
git add moves selected changes to the staging area.
git commit saves these staged changes as a snapshot in the local repository.
git push uploads commits to the remote repository, while git pull brings the latest changes from remote.
git checkout is used to switch branches or restore files.

Flow:
Working Directory → Staging Area → Local Repository → Remote Repository

Basic Steps Using Commands

  1. Initialize or clone a repository

    git init or git clone <url>

  2. Make code changes
    Edit files in your editor

  3. Check status
    git status

  4. Stage changes
    git add .

  5. Commit changes
    git commit -m "message"

  6. Push to GitHub
    git push origin main