Git
This content is for Frontend. Switch to the latest version for up-to-date documentation.
Git is a distributed version control system (VCS). It tracks every change you make to your code, lets you go back in time, and enables multiple people to work on the same project without overwriting each other’s work.
Git is the industry standard — virtually every software team uses it.
Why Version Control?
Section titled “Why Version Control?”Without version control, you end up with:
project-final.zipproject-final-v2.zipproject-FINAL-FINAL.zipproject-FINAL-FINAL-fixed.zipWith Git, you have a clean timeline of every change, who made it, and why.
Core Concepts
Section titled “Core Concepts”Repository (Repo)
Section titled “Repository (Repo)”A repository is a project folder tracked by Git. It contains all your files and the complete history of every change ever made.
# Initialize a new Git repo in the current foldergit initThe Three Areas
Section titled “The Three Areas”Git has three main areas where your files live:
- Working Directory: The files you see and edit on your computer.
- Staging Area (Index): A preparation zone — you choose which changes to include in the next commit.
- Repository: The committed history stored in the
.gitfolder.
Commits
Section titled “Commits”A commit is a snapshot of your project at a point in time. Each commit has:
- A unique hash (ID), e.g.,
a1b2c3d - A message explaining what changed
- A timestamp and author
- A pointer to the parent commit
# Stage specific filesgit add index.html style.css
# Stage everythinggit add .
# Commit with a messagegit commit -m "Add homepage layout and base styles"Checking Status & History
Section titled “Checking Status & History”# See which files are changed, staged, or untrackedgit status
# View commit historygit log
# Compact one-line historygit log --oneline
# See what changed (not yet staged)git diffBranching
Section titled “Branching”Branches let you work on features, fixes, or experiments in isolation without affecting the main codebase.
# Create a new branchgit branch feature/login
# Switch to itgit checkout feature/login
# Or create + switch in one commandgit checkout -b feature/login
# Modern alternative (Git 2.23+)git switch -c feature/loginMerging
Section titled “Merging”When your feature is done, you merge it back into the main branch.
# Switch to maingit checkout main
# Merge the feature branchgit merge feature/login
# Delete the branch (optional, cleanup)git branch -d feature/loginMerge Conflicts
Section titled “Merge Conflicts”A merge conflict happens when two branches changed the same lines in the same file. Git cannot automatically decide which version to keep, so it marks the conflict and asks you to resolve it manually.
<<<<<<< HEAD<h1>Welcome to our site</h1>=======<h1>Welcome to the homepage</h1>>>>>>>> feature/loginYou edit the file to keep the version you want, then:
git add index.htmlgit commit -m "Resolve merge conflict in heading"Common Branching Strategies
Section titled “Common Branching Strategies”| Strategy | Description |
|---|---|
| Feature Branching | One branch per feature. Merge into main when done. Simple and widely used. |
| Git Flow | Uses main, develop, feature/*, release/*, and hotfix/* branches. More structured, common in enterprise. |
| Trunk-Based | Everyone commits to main frequently with small changes. Relies on feature flags and CI. |
Undoing Things
Section titled “Undoing Things”| Command | What it does |
|---|---|
git checkout -- file.txt | Discard uncommitted changes to a file. |
git reset HEAD file.txt | Unstage a file (keep changes in working directory). |
git reset --soft HEAD~1 | Undo the last commit but keep changes staged. |
git reset --hard HEAD~1 | Undo the last commit and discard all changes. Destructive. |
git revert <hash> | Create a new commit that undoes a previous commit. Safe for shared branches. |
git stash | Temporarily save uncommitted changes and clean the working directory. |
git stash pop | Restore stashed changes. |
.gitignore
Section titled “.gitignore”A .gitignore file tells Git which files and folders to not track. This is essential for keeping sensitive data and generated files out of your repo.
# Dependenciesnode_modules/
# Build outputdist/build/
# Environment variables (secrets!).env.env.local
# OS files.DS_StoreThumbs.db
# IDE settings.vscode/.idea/Configuration
Section titled “Configuration”# Set your identity (required for commits)git config --global user.name "Your Name"git config --global user.email "you@example.com"
# Check your configgit config --listWorking with Remotes
Section titled “Working with Remotes”A remote is a version of your repository hosted on a server (like GitHub, GitLab, or Bitbucket).
# Clone an existing repogit clone https://github.com/user/repo.git
# Add a remote to an existing local repogit remote add origin https://github.com/user/repo.git
# Push your commits to the remotegit push origin main
# Pull the latest changes from the remotegit pull origin main
# See configured remotesgit remote -vQuick Reference
Section titled “Quick Reference”git init # Initialize a repogit clone <url> # Clone a remote repogit status # Check file statusgit add . # Stage all changesgit commit -m "message" # Commit staged changesgit push # Push to remotegit pull # Pull from remotegit branch # List branchesgit checkout -b <name> # Create + switch branchgit merge <branch> # Merge branch into currentgit log --oneline # View compact historygit stash # Stash uncommitted changesgit diff # View unstaged changes