Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Brtest Tutorial


Git & Version Control

Version control tracks changes to your code over time so you can collaborate, experiment safely, and roll back mistakes. Git is the most popular version control system and works locally on your computer as well as with remote services like GitHub, GitLab, and Bitbucket.


🔧 Install & First-Time Setup

Install Git from your package manager or official site. Then set your name and email (used in commit history):

Configure Git

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

Tip: Use git config --global -l to see your current settings.


📁 Create a Repository

A repository (repo) is a folder tracked by Git.

Start New Repo

mkdir my-site
cd my-site
git init
echo "# My Site" > README.md
git status

Tip: Create a .gitignore to exclude files (e.g., node_modules, build folders, secrets).

.gitignore (example)

# dependencies
node_modules/
# builds
dist/
build/
# system files
.DS_Store
# env
.env

📝 Stage & Commit Changes

Git tracks changes in three places: working directorystaging arearepository.

Stage & Commit

git add index.html css/style.css
git commit -m "Add homepage and styles"
git log --oneline

Good messages: Use imperative tone: Add header nav, Fix form validation, Refactor gallery.


🌿 Branching & Merging

Branches let you develop features without touching main. Merge when ready.

Feature Branch Workflow

git switch -c feature/navbar
# ...edit files...
git add .
git commit -m "Implement responsive navbar"
git switch main
git merge feature/navbar

Resolve Merge Conflicts (basic)

# Open the conflicted files, keep desired changes, remove conflict markers:
# <<<<<<< HEAD
# (your changes)
# =======
# (their changes)
# >>>>>>> branch-name
git add conflicted-file.html
git commit -m "Resolve merge conflict in header"

🌐 Connect to a Remote (GitHub/GitLab)

Push your local repo to a remote service to back it up and collaborate.

Add Remote & Push

git remote add origin https://github.com/yourname/my-site.git
git branch -M main
git push -u origin main

Clone Existing Repo

git clone https://github.com/yourname/my-site.git
cd my-site
git pull       # fetch and merge remote changes
git push       # upload your new commits

🚀 Typical Web Flow (Solo)

  1. Pull latest (git pull).
  2. Branch for a task (git switch -c feature/foo).
  3. Code, then commit in small steps.
  4. Merge back into main.
  5. Push to remote and deploy.

👥 Team Flow (Pull Requests)

Teams often use PRs (pull/merge requests) for code review.

  1. Create a branch and push it.
  2. Open a PR on the platform (GitHub/GitLab).
  3. Request review, address feedback.
  4. Squash/rebase if needed, then merge.

Tip: Keep branches short-lived. Small, focused PRs are easier to review and revert.


🔄 Undo & Fix Mistakes

Everyone makes mistakes; Git makes it easy to recover.

Common “Oops” Commands

# Unstage a file (keeps changes)
git restore --staged file.css

# Discard local changes in a file (cannot be undone)
git restore file.css

# Amend last commit message/files
git commit --amend

# Go back to a previous commit (safe, keeps history)
git revert <commit-hash>

🏷️ Tags & Releases

Use tags to mark versions (e.g., v1.0.0) and create release notes.

Create & Push Tags

git tag v1.0.0
git push origin v1.0.0
git tag -l

📦 Stash (Shelve WIP)

Temporarily save changes without committing.

Stash Workflow

git stash
git pull
git stash pop

✅ Best Practices

  • Commit often with clear messages.
  • Use branches for features and fixes.
  • Keep main always deployable.
  • Protect main with reviews/status checks (on the platform).
  • Use .gitignore and never commit secrets (.env).
  • Tag releases and keep a CHANGELOG.

🚀 Bonus: Deploy with Git

Many hosts support deploying on git push, or via CI/CD.

Basic CI/CD Concept (Pseudo)

# On push to main:
#   1) Install deps
#   2) Build
#   3) Upload build (or restart server)

Next step: Continue to “WD Git: GitHub Flow” for a step-by-step PR tutorial and to “WD Git: Deploy with Git” to connect your repo to hosting.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.