Chapter 2 of 10 · Git and GitHub for Beginners — Complete Free Tutorial
Installing Git and Initializing a Repository
This second chapter of the Git and GitHub for Beginners series gets your machine ready and walks you through creating your very first repository. By the end you will have Git installed, your identity configured so commits are properly signed, and a project folder that Git is watching.
Installing Git
Git is free, open source and available on every major platform. Installation takes a few minutes, and once the command git works in your terminal you never have to install it again. Pick the row that matches your machine:
| Platform | Fastest route | How to check it worked |
|---|---|---|
| macOS | xcode-select --install, or brew install git if you use Homebrew | Run git --version in Terminal |
| Windows | Installer from git-scm.com, or winget install Git.Git | Open “Git Bash” from the Start menu |
| Linux | sudo apt install git (Debian/Ubuntu) or sudo dnf install git (Fedora) | Run git --version in the shell |
If the version line prints, stop there — do not worry about build options or GUI clients yet. The terminal is the best place to learn, because every graphical tool is a coat of paint over these same commands.
Telling Git who you are
Every commit is stamped with an author name and email, so Git needs to know yours before it will record anything. Configuration is stored as key/value pairs, and the --global flag applies them to every repository on your machine — which is what you want for your identity. Use an email that matches the one on your GitHub account so your commits are linked to your avatar.
Creating your first repository
A repository (or repo) is simply a project folder plus a hidden .git directory where Git stores the entire history. Running git init in a folder switches Git on for that project. The command creates .git and nothing else — no files are recorded until you start committing in the next chapter. Running git init again in the same folder is harmless; Git tells you it is reinitialising an existing repository.
Your first status report
git status is the command you will type more than any other. It answers three questions at once: which branch am I on, what has changed, and what should I do next. On a brand-new repository the answer is honest — there are no commits yet, and every file is untracked, meaning Git can see it but is not recording it. A useful habit is to run status before and after every other command until their output feels automatic.
Ignoring files before they annoy you
Some files never belong in history: dependencies, build output, editor settings and secrets. A .gitignore file at the root of the repository lists patterns Git should silently skip. Ignored files never appear in git status, so your view of the project stays clean from the first commit onwards.
git init bakery-site
Expected output:
Initialized empty Git repository in /Users/sara/Projects/bakery-site/.git/
git config --global user.name "Sara Mehta"
git config --global user.email "sara@example.com"
git config --global --list
Expected output:
user.name=Sara Mehta
user.email=sara@example.com
init.defaultBranch=main
core.autocrlf=input
git status
Expected output:
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
index.html
bakery-menu.txt
nothing added to commit but untracked files present (use "git add" to track)
Read that output slowly: the branch is already named main, Git knows two files exist, and it is telling you exactly which command comes next. That guidance in the status output is your built-in tutor.
Common mistakes
- Configuring without
--global— the identity applies only to one repository, and the next project “forgets” who you are. - Using the wrong email — commits will not match your GitHub account, so your contributions show up unlinked.
- Running
git initinside a folder that is already tracked — nested repositories behave strangely; rungit statusfirst to check. - Skipping
.gitignore— you end up committingnode_modulesor an accidental.envfull of keys. - Expecting Git to record files automatically — it watches them, but only
git addandgit commitput them into history.
Practice checklist
- Install Git, close and reopen your terminal, then confirm with
git --version. - Set
user.nameanduser.emailglobally and verify them withgit config --global --list. - Create a folder called
git-practice, rungit initinside it, then rungit status. - Add a
.gitignorecontainingnode_modules/,.envand.DS_Store, and confirm status stays tidy. - Create one text file, run
git statusagain, and find it listed under “Untracked files”.
What's next?
Your repository exists but its history is empty — time to fill it. Head to Staging and Committing to learn how changes travel from your working folder into a permanent snapshot.
Frequently Asked Questions
Git is a distributed version control system: it records snapshots of your project over time, lets you jump back to any point, branch off to try ideas safely, and merge other people's work. It is the industry standard — nearly every developer job assumes basic Git fluency — and it is the best insurance you can have against lost or broken code.
Git is the tool that runs on your machine and stores history locally; it works fully offline. GitHub is one of many websites (GitLab, Bitbucket) that hosts Git repositories in the cloud and adds collaboration features: pull requests, code review, issues and CI. You can use Git forever without GitHub — GitHub needs Git to be useful.
A repository (repo) is your project folder plus a hidden .git directory containing the entire history: every commit, branch and tag. It can be local (only on your computer) or remote (cloned on GitHub). Deleting .git loses the history but keeps your files; deleting the files while keeping .git leaves history with nothing to show.
git add moves changed files into the staging area — your draft of the next snapshot — so you can review exactly what will be recorded. git commit writes the staged snapshot permanently with a message describing why the change happened. Two steps by design: stage deliberately, commit intentionally.
A branch is a movable pointer to a commit — creating one costs almost nothing. Use a branch for every unit of work (feature, fix, experiment): it keeps unfinished code out of main, lets you review changes in a pull request, and makes abandoning a bad idea as simple as deleting the branch.
It depends on where the change lives. Unstaged edits: git restore file. Staged but uncommitted: git restore --staged file. Committed but not pushed: git reset --soft to keep changes staged. Already pushed: git revert commit — it writes a new commit that undoes the old one instead of rewriting shared history. The undoing chapter covers each case with commands.
A conflict happens when two branches changed the same lines and Git cannot choose automatically. Your file gets marked up with <<<<<<<, ======= and >>>>>>> sections; you edit it down to the correct result, remove the markers, then git add and git commit. Conflicts are normal, small and entirely fixable — they are not an error state.
A pull request (PR) is GitHub's proposal to merge one branch into another. It shows the diff, runs checks, and lets teammates comment line by line before the code lands. It is the centre of modern collaboration: work on a branch, open a PR, review, approve, merge.
Write the subject in imperative mood, under about 50 characters: "Add email validation to signup". Explain why, not what, in the body when it is not obvious. Many teams use Conventional Commits (feat:, fix:, docs:) so tools can generate changelogs — the workflows chapter shows the format in practice.
Secrets (API keys, .env files), dependencies (node_modules), build output (dist), and operating-system junk (.DS_Store, Thumbs.db). A .gitignore file excludes them from the start; the common mistakes chapter shows what to do if something already slipped into history.