Skip to main content

Git

"Distributed version control: track changes, branch, merge"


What is Git?

Git is a distributed version control system. You keep a full history of changes locally; you can branch, merge, and sync with remotes (e.g. GitHub, GitLab).


Memory hook

"Commit = snapshot; branch = parallel line; merge = combine; remote = shared repo"


Core concepts

  • Repository — folder with full history (.git)
  • Commit — snapshot of tracked files at a point in time
  • Branch — movable pointer to a commit (e.g. main, feature/x)
  • Remote — reference to another repo (e.g. origin)
  • Clone — copy a remote repo to your machine
  • Pull — fetch + merge from remote
  • Push — send your commits to remote

Essential commands

Setup and clone

git init                    # create new repo
git clone <url> # clone remote repo
git config user.name "..." # set name (local or global)
git config user.email "..." # set email

Daily workflow

git status                  # see changed/untracked files
git add <file> # stage file(s)
git add . # stage all
git commit -m "message" # create commit
git push # push to remote (e.g. origin main)
git pull # fetch + merge from remote

Branching

git branch                  # list branches
git branch feature/x # create branch
git checkout feature/x # switch branch
git checkout -b feature/x # create and switch
git merge feature/x # merge feature/x into current branch

Viewing history

git log                     # commit history
git log --oneline # short log
git diff # unstaged changes
git diff --staged # staged changes

Undo and reset

git restore <file>          # discard unstaged changes (file)
git restore --staged <file> # unstage file
git reset --soft HEAD~1 # undo last commit, keep changes staged
git reset --hard HEAD~1 # undo last commit, discard changes (careful)

Remote and sync

git remote -v               # list remotes
git fetch origin # get remote refs, no merge
git pull origin main # fetch + merge
git push origin main # push to origin/main
git push -u origin main # push and set upstream

Common workflows

  • Feature branchgit checkout -b feature/name → work → commit → push → open PR → merge
  • Fix on main — pull → fix → commit → push
  • Merge conflict — edit conflicted files → git addgit commit

Interview one-liner

"Git is distributed version control: commits are snapshots, branches are parallel lines of work, merge combines them. Daily flow: add, commit, push, pull; use branches for features and merge via PR."


Cheat sheet

init / clone = start repo
add → commit = snapshot
push / pull = sync with remote
branch / checkout / merge = parallel work
status / log / diff = inspect
restore / reset = undo (careful with --hard)