Git Knowledge Base

Git Basics

Git is a distributed version‑control system that tracks changes in source code during software development. It allows multiple developers to collaborate efficiently.

Key Concepts
  • Repository (repo) – a directory containing all project files and a hidden .git folder that stores history.
  • Commit – a snapshot of the repository at a point in time.
  • Branch – an independent line of development.
  • Merge – combining changes from different branches.
  • Remote – a version of the repository hosted elsewhere (e.g., GitHub).

Setup & Configuration

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global core.editor "code --wait"

Common Workflow

# clone repository
git clone https://github.com/user/project.git

# create a new branch
git checkout -b feature/new-ui

# make changes, then stage them
git add .

# commit changes
git commit -m "Add new UI component"

# push branch to remote
git push origin feature/new-ui

# open a pull request on GitHub

Branching

# list branches
git branch

# switch branch
git checkout develop

# create and switch in one step
git checkout -b hotfix/issue-123

# delete a branch locally
git branch -d feature/old-feature

# delete a remote branch
git push origin --delete feature/old-feature

Remote Repositories

# view configured remotes
git remote -v

# add a new remote
git remote add upstream https://github.com/official/project.git

# fetch updates from remote
git fetch upstream

# integrate fetched changes
git merge upstream/main

Rebase vs Merge

When to Use Rebase

Rebase creates a linear history, ideal for small feature branches before merging.

# rebase current branch onto main
git fetch origin
git rebase origin/main
When to Use Merge

Merge preserves the exact history and is safer for public/shared branches.

# merge main into current branch
git merge main

Undo & Recovery

# undo last commit but keep changes staged
git reset --soft HEAD~1

# undo last commit and unstage changes
git reset --mixed HEAD~1

# discard all local changes
git checkout -- .

# revert a specific commit
git revert a1b2c3d4

Git Cheat Sheet

Command Description
git init Create a new local repository.
git clone <url> Copy an existing repo.
git status Show changed files.
git add <file> Stage changes.
git commit -m "msg" Commit staged changes.
git log View commit history.
git diff Show changes not staged.
git pull Fetch & merge remote changes.
git push Upload local commits.