Git Tutorial for Beginners: Commands, Branches, and GitHub
Learn Git from scratch: installation, commits, branches, merge conflicts, GitHub, and how to undo mistakes without losing your work.

Your final project folder has files like report_v2.docx, report_v2_FINAL.docx, and report_v2_FINAL_for_real.docx. You no longer know which one is right, and the change you made on Tuesday vanished when your teammate sent you “their version.”
That’s how almost everyone worked before Git. Today it’s the tool used by everyone from students to the teams behind Linux, Android, and Visual Studio Code.
In this Git tutorial for beginners you’ll install Git, create your first repository, work with branches, resolve a conflict, push your project to GitHub, and, above all, undo mistakes without losing anything. Open a terminal and type the commands along with me.
What Is Git and Why Do Almost All Development Teams Use It?
Git is a distributed version control system. It saves snapshots (commits) of your project’s state, and you can go back to any of them whenever you want. Linus Torvalds created it in 2005 to coordinate development of the Linux kernel.
“Distributed” means every person has a full copy of the history on their own computer. You can work offline, create branches to experiment, and sync with your team only when you’re ready.
Git Is Not GitHub: The Difference in 30 Seconds
This is the number one source of confusion for beginners. Git is the program you install; GitHub is a web service where you store a copy of your repositories to collaborate.
| Tool | What it is | What you use it for |
|---|---|---|
| Git | Version control program (free and open source) | Tracking changes, creating branches, and undoing mistakes on your machine |
| GitHub | Web platform owned by Microsoft | Hosting repositories, reviewing code with pull requests, and showcasing a portfolio |
| GitLab | Web platform that can also be self-hosted | Same as GitHub, with tightly integrated CI/CD |
| Bitbucket | Web platform from Atlassian | Teams already using Jira and other Atlassian tools |
You can use Git your whole life without opening GitHub. But GitHub without Git makes no sense.
How to Install Git on Windows, macOS, and Linux
Installation takes a couple of minutes. Pick your system:
# Windows: official installer (includes Git Bash) from https://git-scm.com
# or, from PowerShell, with the winget package manager:
winget install --id Git.Git -e
# macOS: install Xcode's command line tools...
xcode-select --install
# ...or use Homebrew
brew install git
# Ubuntu / Debian
sudo apt update && sudo apt install git
# Fedora
sudo dnf install git
Check the installation with git --version. If you see something like git version 2.x, you’re ready.
Windows tip: the Git for Windows installer ships with Git Bash, a terminal that uses the same commands as Linux and macOS. If you follow online courses, it will spare you plenty of syntax differences.
Initial Setup: Name, Email, and Default Branch
Git signs every commit with your name and email. Set them once:
git config --global user.name "Your Name"
git config --global user.email "you@email.com"
# Name the default branch of new repositories "main" (Git 2.28 or later)
git config --global init.defaultBranch main
# Optional: use VS Code to write longer messages
git config --global core.editor "code --wait"
# Review your configuration
git config --list
Use the same email you register on GitHub so your commits are linked to your profile.
Git’s Areas: Working Directory, Staging, and Repositories
Before typing commands, understand where your code “lives.” This idea prevents most early confusion.
- Working directory: the files you see and edit in your folder.
- Staging area: a “waiting room” where you choose which changes go into the next commit.
- Local repository: the commit history, stored in the hidden
.gitfolder. - Remote repository: a copy on GitHub or another server, for sharing and backup.

Why does staging exist? Because it lets you make small, meaningful commits. If you changed three files for different reasons, you can save them as three separate commits.
Your First Git Repository, Step by Step
Create the Repository with git init
mkdir my-project
cd my-project
git init
Git replies Initialized empty Git repository in .../my-project/.git/. That .git folder is your history: don’t delete it or edit it by hand.
Git Add, Commit, and Log: The Basic Workflow
Create a file and watch Git detect it:
echo "# My project" > README.md
git status
git status will show it in red as untracked. Move it to staging and make your first commit:
git add README.md # a specific file
git add . # or every change in the folder
git commit -m "Add initial README"
git log --oneline # compact history
# a1b2c3d Add initial README
That’s the loop you’ll repeat hundreds of times: edit → git add → git commit. Run git status constantly; it’s your map.
To see exactly what changed before saving, use git diff (unstaged changes) or git diff --staged (changes already staged).
How to Write Good Commit Messages
Six months from now, your commit messages will be the only explanation of why the code changed. Write them for your future self:
| Avoid | Prefer |
|---|---|
changes |
Fix tax calculation in the cart |
misc fixes |
Validate email before submitting the form |
asdf |
Add button to download the report as PDF |
final final 2 |
Update security dependencies |
The most common convention: an imperative verb, one idea per commit, and roughly 50 characters or fewer on the first line.
Ignore Files with .gitignore
Some files should never enter the repository: downloaded dependencies, generated files, and above all passwords and API keys. Create a .gitignore file at the root:
node_modules/
dist/
.env
*.log
.DS_Store
Git will ignore everything matching those patterns. GitHub maintains .gitignore templates for almost every language.
Git Branches: Create, Switch, and Merge
A branch is an independent line of work. It lets you try an idea without touching the code that already works on main.
# Create a branch and switch to it
git switch -c feature/contact-form
# ...edit, add, and commit on the branch...
# Go back to the main branch
git switch main
# Bring the branch's changes into main
git merge feature/contact-form
# Delete the branch when you no longer need it
git branch -d feature/contact-form
Older tutorials use git checkout -b instead of git switch -c. They do the same thing; git switch has existed since Git 2.23 and is easier to remember because it only switches branches.
What to Do When You Hit a Merge Conflict
A conflict happens when two branches changed the same line in different ways. Git won’t guess which one is right, so it asks you to decide. The file will look like this:
<<<<<<< HEAD
<h1>Welcome to my portfolio</h1>
=======
<h1>Hi, I'm a web developer</h1>
>>>>>>> feature/new-title
To resolve it:
- Open the file and decide which text stays (one, the other, or a mix).
- Delete the
<<<<<<<,=======, and>>>>>>>markers. - Save the file and run
git add file.html. - Finish with
git commit.
Stay calm: a conflict is not an error and doesn’t break anything. If you get nervous halfway through,
git merge --abortputs everything back the way it was before the merge.
How to Push Your Project to GitHub
Create an empty repository on GitHub (without a README, to avoid conflicts with yours) and copy its URL. Then connect your local repository:
git remote add origin https://github.com/your-username/my-project.git
git push -u origin main
The -u flag remembers the connection; from then on git push is enough. To bring in changes other people pushed, use git pull. To download an existing repository from scratch, use git clone URL.
About authentication: GitHub doesn’t accept your account password for Git operations over HTTPS. You have three options:
- Git Credential Manager, bundled with Git for Windows: it opens the browser so you can sign in.
- GitHub CLI (
gh auth login): sets up access in a couple of steps. - SSH keys: the favorite of people who work with several repositories every day.
A Professional Workflow with Pull Requests
On teams, almost nobody commits directly to main. The usual flow is:
- Create a branch from an up-to-date
main(rungit pullfirst). - Make small, descriptive commits.
- Push the branch with
git push -u origin branch-name. - Open a pull request on GitHub so someone can review the code.
- Apply the changes requested in the review.
- Merge into
mainand delete the branch.
Even if you work alone, this flow helps: pull requests document why you made each change, and they look great in a portfolio.
How to Undo Changes in Git Without Losing Work
This is where Git proves its worth. Pick the command that matches your situation:
| Situation | Command |
|---|---|
| I want to discard changes in a file (not committed) | git restore file.js |
| I added a file to staging by mistake | git restore --staged file.js |
| I got the last commit message wrong (not pushed) | git commit --amend -m "Correct message" |
| I want to undo the last commit but keep the changes | git reset --soft HEAD~1 |
| I want to revert a commit I already pushed to GitHub | git revert a1b2c3d |
| I need to set aside unfinished changes to switch branches | git stash, then git stash pop |
| I want to know who changed a line and when | git blame file.js |
Warning:
git reset --harddeletes changes without asking, and rewriting commits you’ve already shared (withresetor--amendfollowed bypush --force) breaks your teammates’ history. If the commit is already on GitHub, usegit revert: it creates a new commit that undoes the previous one without rewriting anything.
Deleted something and think it’s gone? Try git reflog. It shows every recent move in your repository, including commits that “disappeared,” and it almost always lets you recover them.
Common Mistakes and Git Limitations Worth Knowing
Git is powerful, but it isn’t magic. These are the most frequent stumbles and limitations:
- Committing secrets by accident. If an API key lands in a commit, it stays in the history even if you delete the file later. Revoke the key immediately and use
.gitignorefrom day one. - Git alone is not a backup. Your history lives on your disk; if it fails and you never pushed, you lose everything.
- It isn’t designed for large binary files. Videos, datasets, or multi-gigabyte models bloat the repository. That’s what Git LFS (Large File Storage) is for.
- Giant commits. A commit with 40 files and the message “progress” is impossible to review or revert. Several small ones are better.
- The learning curve is real. The first few days are confusing; after two weeks of daily use, the
add→commit→pushloop becomes second nature.
If you work with microcontrollers, Git pairs well with simulators like Wokwi, which integrates with VS Code: you can version your Arduino or ESP32 code just like any web project.
Next Steps to Master Git and GitHub
With what you just practiced, you already cover 90% of everyday Git use: creating repositories, making meaningful commits, working on branches, resolving conflicts, collaborating on GitHub, and recovering from mistakes.
To go deeper, the Pro Git book is free and is the official reference. And if you’re looking for projects to practice on, freeCodeCamp courses give you ideal exercises to version from day one.
Your challenge for today: create a repository for your next project, make at least three commits with good messages, and push it to GitHub. Which Git command was hardest for you to understand? Tell us in the comments.
CuriosoTech Team
Equipo editorial de CuriosoTech. Preparamos guías de tecnología y educación con fuentes oficiales y las actualizamos cuando algo cambia.
Tags
Comments