Git for Beginners: Basics and Essential Commands

What is Git
- Git is a distributed version control system (DVCS). “Distributed” means every developer has a full copy of the repository history on their machine, so you can commit, branch, and inspect history offline. Git tracks changes over time, letting you see who changed what, when, and why—and roll back if needed.
Why Git is Used
Collaboration: multiple people can work on the same codebase without overwriting each other.
History and accountability: every change is recorded with an author, timestamp, and message.
Safety: you can roll back to earlier commits if something breaks.
Branching and experimentation: create branches to try features or fixes without risking the main line; merge when ready.
Speed and offline work: local operations are fast and don’t require a network connection.
Git Basics and Core Terminologies
Repository (repo): the project plus its version history. A “local repo” lives on your machine; a “remote repo” (e.g., GitHub) is shared.
Commit: a snapshot of tracked files at a point in time, with metadata (author, message, timestamp, unique hash).
Branch: a movable pointer to a line of development. main (or master) is the default primary branch; feature branches let you work in isolation.
HEAD: your current checked-out commit/branch. HEAD usually points to a branch, which points to a commit.
Staging area (index): where you prepare changes (git add) before creating a commit.
Working directory: your files on disk. Changes here are unstaged until you add them.
Common Git Commands
Start a repo:
git init — create a new repo in the current folder.
git clone — copy an existing remote repo locally.
Inspect state:
git status — see what’s changed, staged, or untracked.
git log — view commit history (add --oneline for brevity).
Stage and commit:
git add — stage a file.
git add . — stage all tracked/untracked changes in the current directory.
git commit -m "message" — create a commit from staged changes.
Branching and switching:
git branch — list branches.
git switch -c — create and switch to a new branch. (Older form: git checkout -b )
git switch — move to an existing branch.
Sync with remotes (once a remote like origin is set):
git remote add origin — add a remote named origin.
git push -u origin — push your branch and set upstream.
git pull — fetch and merge changes from the remote tracking branch.
git fetch — download remote refs without merging.
Viewing differences:
git diff — show unstaged changes.
git diff --staged — show staged changes.
Undo basics (safe starters):
git restore — discard unstaged changes to a file.
git restore --staged — unstage a file (keep working copy changes).
git reset --soft — move HEAD/branch to a commit, keep changes staged (use carefully).
git reset --hard — move HEAD/branch and discard changes (dangerous; avoid as a beginner unless certain).
A simple “from scratch” workflow example
Create or clone a repo:
- git init (or git clone )
Check status:
- git status
Make changes to files.
Stage changes:
- git add . (or git add )
Commit:
- git commit -m "Add initial feature"
Review history:
- git log --oneline
Create a feature branch:
- git switch -c feature/login-page
(work, git add, git commit as before)
- git switch -c feature/login-page
Push to remote and set upstream (first push of this branch):
- git push -u origin feature/login-page
Update your local branch from its remote:
- git pull (or git fetch; then merge/rebase if needed)






