Git and GitHub Explained for Beginners (With Real Examples)

Git and GitHub for beginners
Version control · Beginner guide · Hands-on

If you have ever saved a file as report_final_v2_REAL_final.docx, you already understand why Git exists. It is the grown-up version of that habit: a tool that remembers every version of your work, lets you travel back in time, and lets a team edit the same project without overwriting each other. GitHub is where those projects usually live online. This guide explains both from zero.

Reading time: about 18 minutesLevel: complete beginnerTools: a terminal and a free GitHub account

The problem Git solves

I started programming long before Git was everywhere, and I remember what folders looked like in those days. site_backup, site_backup_old, site_NEW, site_NEW_fixed, and one called site_dont_touch. Nobody knew which was current. When a client said “the version from last Tuesday was better,” the honest answer was often “I think I overwrote it.”

Now picture a team of five editing the same files. Two people change the same paragraph. Someone emails a zip file. Someone else works from an older copy. Merging all of that by hand is miserable, and mistakes are guaranteed.

Version control is the fix. Instead of copying folders, you tell a tool when you have reached a meaningful point, and it stores a snapshot. The most popular version control system in the world is Git, created in 2005 by Linus Torvalds for the Linux kernel. Today it is used by solo students, small studios and the largest software companies alike.

The one-sentence version. Git is a tool on your computer that records snapshots of your project over time. GitHub is a website that stores your Git projects online so you can back them up, share them and collaborate.

Git vs GitHub: the part everyone mixes up

Beginners often use the two names as if they were one thing. They are not, and the difference matters.

  • Git is software that runs on your own computer. It works offline. It does the actual tracking of changes.
  • GitHub is a company’s website (owned by Microsoft) that hosts Git repositories in the cloud and adds features around them: pull requests, issue tracking, code review and project pages.

The analogy I use in workshops: Git is a camera, and GitHub is the photo-sharing site. You can take photos without ever uploading them, but the website makes it easy to back them up and show them to others. Similar sites exist, including GitLab and Bitbucket. They all speak Git.

QuestionGitGitHub
What is it?A version-control programA hosting website and platform
Where does it run?On your computerOnline, in the cloud
Needs internet?NoYes
Main jobTrack and organise your changesStore, share and review them with others
AlternativesMercurial, SubversionGitLab, Bitbucket

The vocabulary you need (and nothing more)

Git has a reputation for jargon. In reality, you need about eight words, and each has a plain-English meaning.

TermPlain meaningEveryday analogy
repositoryA project folder that Git tracks, with its full historyA filing cabinet with every past version
commitA saved snapshot with a messageA save point in a video game
branchA separate line of workA parallel draft you can experiment in
mergeCombining one branch into anotherPasting your draft into the main document
cloneCopying a repository to your computerDownloading the whole cabinet
remoteA copy of the repository hosted elsewhereThe cloud backup
push / pullSend your commits up / bring others’ commits downUpload and download
pull requestA request to merge your branch, with review“Please check my work before it goes in”

How a change travels through Git

This is the single most useful mental model in the whole subject, and almost every beginner tutorial skips it. A change passes through four places.

Working folderfiles you editStaging areawhat goes nextLocal repoyour saved historyGitHubcopy in the cloudaddcommitpushpullThe first three live on your computer. Only the fourth is online.YOUR COMPUTER
Edit in the working folder, stage what you want, commit it to history, then push it online.
  • Working folder: the files you see and edit on your computer.
  • Staging area: a waiting room where you choose exactly which changes go into the next snapshot.
  • Local repository: the saved history of commits, stored in a hidden .git folder.
  • Remote (GitHub): the copy online that teammates can reach.

The staging area surprises people. Why not save everything at once? Because it lets you craft tidy commits. Imagine you fixed a typo and also started a risky new feature in the same afternoon. Staging lets you commit the typo fix by itself, with a clear message, and leave the unfinished feature for later.

Setting up: install and introduce yourself

Download Git from the official site (git-scm.com) or install it with your system’s package manager. Then check that it works.

git --version

Next, tell Git who you are. Every commit carries a name and email, so this is a one-time setup.

git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global init.defaultBranch main

The last line makes new repositories start with a branch called main, which matches what GitHub uses today. Older installs may still default to master, so setting it avoids confusion.

Your first repository, step by step

Let us track a small recipe collection. Open a terminal, make a folder and start Git inside it.

mkdir recipes
cd recipes
git init

Git replies with something like:

Initialized empty Git repository in /home/you/recipes/.git/

Now create a file called pasta.txt with three lines of text in any editor, then ask Git what it sees.

git status

On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	pasta.txt

nothing added to commit but untracked files present (use "git add" to track)

“Untracked” means Git sees the file but is not recording it yet. git status is your best friend: run it constantly, especially when confused. Now stage the file and commit it.

git add pasta.txt
git commit -m "Add pasta recipe"

[main (root-commit) a1b2c3d] Add pasta recipe
 1 file changed, 3 insertions(+)
 create mode 100644 pasta.txt

Congratulations, you have made your first snapshot. The odd string a1b2c3d is the start of a commit’s unique ID. (Yours will look different; the examples here use made-up IDs.) Add a sauce recipe and a typo fix the same way, and your history looks like this.

git log --oneline

1f2e3d4 (HEAD -> main) Add dessert
7c8d9e0 Fix typo
e4f5a6b Add sauce
a1b2c3d Add pasta recipe
a1b2c3dAdd pastae4f5a6bAdd sauce7c8d9e0Fix typo1f2e3d4Add dessertmain ← HEAD
Each commit points back to the one before it. HEAD marks where you are now.

Every dot in that chain is a version you can return to. That is the whole magic: history becomes a thing you can read, search and travel through.

Writing commit messages people will thank you for

A commit message is a note to your future self, and to everybody else. Compare these two histories:

  • “stuff”, “fix”, “fix 2”, “asdf”
  • “Add login form validation”, “Fix crash when email is empty”, “Remove unused styles”

The second tells a story. A few habits help: write in the imperative (“Add”, not “Added”), keep the first line under about 50 characters, and describe what and why, not how. Six months later, when something breaks, you will be searching that history for answers, and clear messages turn a two-hour hunt into a two-minute one.

Telling Git what to ignore

Not every file belongs in version control. Passwords, API keys, temporary files and huge generated folders should stay out. Create a plain text file named .gitignore and list patterns.

.env
node_modules/
*.log
.DS_Store

This is more than tidiness. Never commit secrets such as API keys or passwords. Once something is pushed to a public repository, bots can find it within minutes. If it happens, rotate the key immediately. Deleting the file later does not erase it from history.

Branches: safe places to experiment

A branch is simply a movable label on a line of commits. Your main branch holds the version that works. When you want to try something, you create a new branch, work there, and leave main untouched.

git switch -c feature/bigger-portions
# edit files, then
git add pasta.txt
git commit -m "Increase pasta portion size"

Think of it as photocopying a document to scribble on, with the guarantee that the original stays clean. If the idea works, you merge it in. If it does not, you delete the branch and nothing is lost.

mainfeature/searchmerge commitWork on the side, then join it back. Main stays safe in the meantime.
A feature branch splits off main, collects its own commits, and merges back.

When you are ready, switch back to main and merge.

git switch main
git merge feature/bigger-portions

Merge conflicts are not scary

Sometimes two branches change the same line. Git cannot guess which you want, so it stops and asks you to decide. It marks the file like this.

<<<<<<< HEAD
Use 200g of spaghetti
=======
Use 250g of spaghetti
>>>>>>> feature/bigger-portions

The top part is your current branch, the bottom part is the incoming one. Edit the file to keep what you want (and delete the marker lines), then git add it and git commit. A conflict is just Git being polite and asking a question. In teams, conflicts become rare once people commit small changes often and pull regularly.

Bringing in GitHub

So far everything has lived on your computer. Now let us put it online. Create a free account at github.com, click the button to create a new repository, and name it recipes. GitHub will show you the web address of your empty repository. Connect your local project to it and upload.

git remote add origin https://github.com/YOU/recipes.git
git branch -M main
git push -u origin main

Here origin is just the nickname Git gives to your main remote. The -u flag remembers the connection so that later you can type only git push and git pull.

A practical note on logging in: GitHub no longer accepts your account password for Git operations over HTTPS. Use a personal access token, set up SSH keys, or install the GitHub CLI or Git Credential Manager, which handle sign-in for you. Their setup pages walk through it in a few minutes.

To get a project that already exists on GitHub onto your machine, you clone it.

git clone https://github.com/YOU/recipes.git

That downloads every file and the whole history, and sets up origin automatically.

Pull requests: how teams actually work

On a team, nobody pushes straight to main. Instead, the loop looks like this.

1 Branchgit switch -c2 Commitgit commit3 Pushgit push4 Pull requestreview5 Mergethen pullThe loop almost every team repeats several times a day.
Branch, commit, push, open a pull request, merge. Repeat.

A pull request (often shortened to PR) is a page on GitHub that shows exactly what changed between your branch and main. Teammates can comment on specific lines, request changes and approve. When everyone is happy, someone clicks Merge. Automated checks, such as tests, can run on the PR too, so a broken change is caught before it reaches the shared code.

I have watched this process save projects. A new developer once pushed a change that would have deleted a customer table. Two people spotted it in review because the diff showed hundreds of red lines where there should have been five. Without a pull request, that change would have gone straight to production.

Five real situations, five workflows

Tap through the tabs. Each one shows a realistic scenario with the actual commands.

Solo project

You are writing a small recipe book as text files and want a safety net. No team, no internet needed.

git init
git add pasta.txt
git commit -m "Add pasta recipe"
git log --oneline
Commands4
RiskNone
WhereYour PC
Needs GitHubNo

Why it works. Each commit is a snapshot you can return to. After a month of edits you can look back and see exactly when the sauce recipe changed, and why.

Undo mistakes

You edited the wrong file, or committed something you regret. Git has a different tool for each size of mistake.

# discard edits you have not committed
git restore pasta.txt

# take a file back out of the staging area
git restore --staged pasta.txt

# undo a commit by adding a new opposite commit
git revert 7c8d9e0
Safestrestore
Safe on sharedrevert
Dangerousreset –hard
TimeSeconds

Why it works. Use restore for uncommitted edits, and revert when the commit has already been shared, because it leaves history intact. Treat reset –hard as the sharp knife: it can throw work away.

Team feature

You and three colleagues are adding a search bar to a website. Nobody should break the working version.

git switch -c feature/search
# ...edit files...
git add .
git commit -m "Add search box"
git push -u origin feature/search
# open a pull request on GitHub, get a review, merge
git switch main
git pull
Branches1 each
ReviewPull request
Safe mainYes
People2 to 20

Why it works. Everyone works on their own branch, then proposes changes with a pull request. A teammate reads the changes before they reach main. That review step catches more bugs than most people expect.

Open source

You found a typo in a popular project’s documentation and want to fix it, but you are not a maintainer.

# click Fork on GitHub, then:
git clone https://github.com/YOU/project.git
cd project
git switch -c fix-typo
# fix the typo, then
git commit -am "Fix typo in README"
git push -u origin fix-typo
# open a pull request to the original project
Step oneFork
Step twoClone
ThenBranch
FinishPull request

Why it works. A fork is your personal copy of someone else’s repository on GitHub. You change your copy and ask the owners to pull your change in. This is how thousands of strangers contribute to the same project.

Publish a site

You built a one-page portfolio and want it online without paying for hosting.

git add index.html
git commit -m "Launch portfolio"
git push origin main
# On GitHub: Settings, Pages, deploy from branch main
HostGitHub Pages
CostFree tier
Filesindex.html
Updategit push

Why it works. GitHub Pages serves static files straight from a repository. After setup, every push updates the live site. Your history doubles as a deployment log.

You will notice the same handful of commands appearing in every tab. That is the good news: the entire daily workflow of most developers is built from perhaps ten commands.

A beginner’s cheat sheet

CommandWhat it doesWhen to use it
git statusShows what has changed and what is stagedConstantly. Any time you are unsure.
git add <file>Stages a file for the next commitBefore every commit
git commit -m “msg”Saves a snapshot of staged changesAfter each small, finished step
git log –onelineLists past commits in short formTo see history
git diffShows exact line changes not yet stagedBefore staging, to review your work
git switch -c nameCreates and switches to a new branchStarting a new task
git merge nameMerges a branch into the current oneFinishing a task locally
git pushUploads commits to the remoteSharing or backing up your work
git pullDownloads and merges remote changesStart of your working day
git clone <url>Copies a remote repository to your computerJoining an existing project
git restore <file>Discards uncommitted edits to a fileWhen an experiment went wrong
git revert <id>Undoes a commit by adding an opposite oneUndoing something already shared

A daily routine you can copy

  • Morning: run git switch main and git pull so you start from the latest version.
  • Before starting a task: create a branch with git switch -c and a descriptive name.
  • While working: run git status and git diff often, and commit each small finished step.
  • When done: push the branch and open a pull request.
  • After the merge: switch back to main, pull, and delete the old branch.

It feels like a lot at first, and then one day you realise you have stopped thinking about it. That is when Git becomes a superpower instead of a chore.

Seven mistakes beginners make (tap to open)

1. Committing everything with a vague message

Messages like “update” tell nobody anything. Take ten seconds to describe the change. Your future self is the main beneficiary.

2. Committing secrets and API keys

Add sensitive files to .gitignore from day one. If a key leaks, revoke it immediately; deleting the file later does not remove it from history.

3. Working directly on main

Even solo, branches give you a safe place to experiment. On teams, working on main is how broken code reaches everyone.

4. Forgetting to pull before pushing

If a teammate has pushed since you last pulled, your push may be rejected. Pull first, resolve any conflicts, then push.

5. Using git reset –hard without thinking

It can destroy uncommitted work permanently. Prefer git restore for single files and git revert for shared commits unless you are certain.

6. Giant commits that mix unrelated changes

A commit with a bug fix, a refactor and a new feature is hard to review and hard to undo. Stage and commit them separately.

7. Panicking at a merge conflict

A conflict is a question, not a failure. Read the markers, choose what to keep, remove the markers, add, and commit.

Quick quiz: test yourself

Tap each question to reveal the answer and the reasoning.

What is the difference between Git and GitHub?
  1. They are the same thing
  2. Git is a website and GitHub is a program
  3. Git is the version-control tool on your computer, GitHub is an online home for Git repositories
  4. GitHub is only for photographers

Git tracks changes locally. GitHub hosts repositories online and adds collaboration features such as pull requests.

Which command moves changes from the working folder into the staging area?
  1. git add
  2. git commit
  3. git push
  4. git clone

git add stages changes. git commit then saves the staged snapshot into history.

You edited a file but have not committed. How do you throw those edits away safely?
  1. git push –force
  2. git restore <file>
  3. git merge
  4. git fork

git restore puts the file back to its last committed state. Be sure you want to lose those edits first.

What is a pull request?
  1. A command that downloads files
  2. A way to delete a branch
  3. A backup of your computer
  4. A proposal to merge your branch, with a place for review and discussion

A pull request asks the project to pull your changes in, and lets teammates review them first.

Which file stops Git from tracking things like passwords and build folders?
  1. README.md
  2. LICENSE
  3. .gitignore
  4. package.json

List patterns in .gitignore for files Git should leave alone. Secrets that were already committed need extra steps, so never commit them in the first place.

Frequently asked questions

Do I need GitHub to use Git?

No. Git works completely on your own computer. GitHub is optional, but it makes backup, sharing and teamwork much easier. GitLab and Bitbucket are alternatives.

What is a repository?

A repository, or repo, is a project folder that Git tracks, along with the full history of its changes stored in a hidden .git folder.

What is the difference between git pull and git fetch?

git fetch downloads new history from the remote without changing your files. git pull fetches and then merges it into your current branch.

What does git clone do?

It copies an entire repository from a remote location such as GitHub to your computer, including its history, and sets up the connection called origin.

How often should I commit?

Commit whenever you finish a small, meaningful step that you could describe in one sentence. Small commits are easier to review and easier to undo.

What if I committed a password by mistake?

Treat it as compromised: change the password or revoke the key right away. Removing it from history is a separate, harder job, so rotating the secret comes first.

The takeaway

Git remembers every version of your project, GitHub keeps a copy online and helps people work together, and the daily routine boils down to a few commands: status, add, commit, push, pull, and switch. You do not need to master everything. You need to commit small, write clear messages, work on branches and never commit secrets.

Make a practice repository today. Add three files, make five commits, create a branch, merge it, push it to GitHub. An hour of doing beats a week of reading, and you will never again have a folder called “final_v2_REAL_final.”

GitGitHubversion controlgit commandspull requestfor beginners

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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