Skip to content
UNASPACE

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.

Without version control, you end up with:

project-final.zip
project-final-v2.zip
project-FINAL-FINAL.zip
project-FINAL-FINAL-fixed.zip

With Git, you have a clean timeline of every change, who made it, and why.

A repository is a project folder tracked by Git. It contains all your files and the complete history of every change ever made.

Terminal window
# Initialize a new Git repo in the current folder
git init

Git has three main areas where your files live:

Diagram
  1. Working Directory: The files you see and edit on your computer.
  2. Staging Area (Index): A preparation zone — you choose which changes to include in the next commit.
  3. Repository: The committed history stored in the .git folder.

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
Terminal window
# Stage specific files
git add index.html style.css
# Stage everything
git add .
# Commit with a message
git commit -m "Add homepage layout and base styles"
Terminal window
# See which files are changed, staged, or untracked
git status
# View commit history
git log
# Compact one-line history
git log --oneline
# See what changed (not yet staged)
git diff

Branches let you work on features, fixes, or experiments in isolation without affecting the main codebase.

Diagram
Terminal window
# Create a new branch
git branch feature/login
# Switch to it
git checkout feature/login
# Or create + switch in one command
git checkout -b feature/login
# Modern alternative (Git 2.23+)
git switch -c feature/login

When your feature is done, you merge it back into the main branch.

Terminal window
# Switch to main
git checkout main
# Merge the feature branch
git merge feature/login
# Delete the branch (optional, cleanup)
git branch -d feature/login

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/login

You edit the file to keep the version you want, then:

Terminal window
git add index.html
git commit -m "Resolve merge conflict in heading"
StrategyDescription
Feature BranchingOne branch per feature. Merge into main when done. Simple and widely used.
Git FlowUses main, develop, feature/*, release/*, and hotfix/* branches. More structured, common in enterprise.
Trunk-BasedEveryone commits to main frequently with small changes. Relies on feature flags and CI.
CommandWhat it does
git checkout -- file.txtDiscard uncommitted changes to a file.
git reset HEAD file.txtUnstage a file (keep changes in working directory).
git reset --soft HEAD~1Undo the last commit but keep changes staged.
git reset --hard HEAD~1Undo 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 stashTemporarily save uncommitted changes and clean the working directory.
git stash popRestore stashed changes.

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.

# Dependencies
node_modules/
# Build output
dist/
build/
# Environment variables (secrets!)
.env
.env.local
# OS files
.DS_Store
Thumbs.db
# IDE settings
.vscode/
.idea/
Terminal window
# Set your identity (required for commits)
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# Check your config
git config --list

A remote is a version of your repository hosted on a server (like GitHub, GitLab, or Bitbucket).

Terminal window
# Clone an existing repo
git clone https://github.com/user/repo.git
# Add a remote to an existing local repo
git remote add origin https://github.com/user/repo.git
# Push your commits to the remote
git push origin main
# Pull the latest changes from the remote
git pull origin main
# See configured remotes
git remote -v
Terminal window
git init # Initialize a repo
git clone <url> # Clone a remote repo
git status # Check file status
git add . # Stage all changes
git commit -m "message" # Commit staged changes
git push # Push to remote
git pull # Pull from remote
git branch # List branches
git checkout -b <name> # Create + switch branch
git merge <branch> # Merge branch into current
git log --oneline # View compact history
git stash # Stash uncommitted changes
git diff # View unstaged changes
Built with passion by Ngineer Lab