Web Development

Git and GitHub for Beginners

Git vs. GitHub

Git is version control software that runs on your computer, tracking changes to your files over time. GitHub is a website that hosts Git repositories online so you can back them up and collaborate with others. They’re related but not the same thing.

Setting Up a Repository

git init git add . git commit -m "Initial commit"

The Core Workflow

git status # see what changed git add file.txt # stage a specific file git commit -m "Fix header bug" git push origin main # send commits to GitHub

Cloning an Existing Project

git clone https://github.com/username/repo.git cd repo

Branching

Branches let you work on a feature without affecting the main codebase until you’re ready to merge:

git branch feature-login git checkout feature-login # or in one step: git checkout -b feature-login

Merging Changes

git checkout main git merge feature-login

Undoing Mistakes

git checkout -- file.txt # discard unstaged changes git reset HEAD file.txt # unstage a file git revert <commit-hash> # safely undo a pushed commit

Practice Exercise

Create a new repository, make three commits with meaningful messages, create a branch, make a change there, and merge it back into main.

Leave a Reply

Your email address will not be published. Required fields are marked *