Your Files Deserve a Time Machine

You’ve been there. You edited a report for an hour, saved over it, and realized you deleted three paragraphs you can’t get back. Or you kept folders called final_v1, final_v2_REAL, and final_v2_REAL_USE_THIS. Git fixes that problem. Think of Git as a free time machine for your files: every time you make a meaningful change, you take a snapshot. If you later break something, you can rewind to any earlier snapshot.

This git for beginners guide explains what is git, shows you git vs github, and walks through the core git init add commit workflow. By the end, you’ll save versions of a project and undo mistakes without crying.

What Is Git (and What Is GitHub)?

Git is a free, open-source version control tool. Linus Torvalds created it in 2005, and as of 2026, more than 90% of developers use it. Git runs on your computer and records snapshots of your project in a hidden folder called .git. You don’t need the internet to use it.

GitHub is a website where you can store Git repositories in the cloud. It gives you a backup copy, a place to share code, and tools for working with other people. You can use Git without GitHub, but GitHub makes collaboration and offsite backup easier.

Here is a simple git vs github comparison:

Git GitHub
Software installed on your computer Cloud service for hosting Git repositories
Tracks versions locally Stores your project online
Works fully offline Needs an account and internet for most features
Created by Linus Torvalds in 2005 Launched later as a hosting platform for Git repos
Free and open source Free accounts available; paid plans optional

Install and Set Up Git

If you’re brand new to the terminal, review the Linux terminal basics guide before you start. Then open your terminal.

On Ubuntu or Debian, run:

# Refresh your package list
sudo apt update

# Install Git
sudo apt install git

On macOS, use Homebrew:

# Install Git with Homebrew
brew install git

You can also download the installer from git-scm.com if you prefer a graphical setup.

Verify the installation:

# Check the installed Git version
git --version

Now tell Git who you are. These labels get attached to every snapshot you make.

# Replace Your Name with your actual name or a username
git config --global user.name "Your Name"

# Replace you@example.com with your email address
git config --global user.email "you@example.com"

Your First Git Project in 5 Minutes

Let’s run the full git init add commit sequence. Create a project folder, turn it into a Git repository, add a file, and commit it.

# Create a new project folder and move into it
mkdir my-first-project
cd my-first-project

# Tell Git to start tracking this folder
# This creates a hidden .git directory inside it
git init

# Create a simple README file
echo "# My First Git Project" > README.md

# See what Git notices in the working directory
git status

# Add README.md to the staging area
git add README.md

# Commit the staged file as a permanent snapshot
git commit -m "First commit: add README"

# View the commit history in one line per commit
git log --oneline

That is the everyday Git flow: make changes, stage them with git add, and save them with git commit.

The Daily Git Loop: edit, add, commit

Git uses three areas for your files. Imagine your working directory is your desk, the staging area is a box where you put papers you’re ready to file, and the repository is a filing cabinet. You work at the desk, place finished papers into the box, and then file them in the cabinet.

  • Working directory: the actual files you edit.
  • Staging area: a chosen set of changes that will go into the next snapshot.
  • Repository: the stored history of all committed snapshots.

Each day, you repeat three steps:

# 1. Edit files in your project normally

# 2. Check what changed
git status

# 3. Stage the files you want to save
git add notes.txt

# 4. Commit the staged changes with a short message
git commit -m "Add meeting notes"

git status is your best friend. Run it any time you’re confused. It tells you what’s modified, what’s staged, and what Git sees.

Undo Your Mistakes

Here are the four most useful undo commands for beginners. Some are safe, and some require a little care.

If you edited a file in the working directory and want to throw away those uncommitted changes:

# Discard unstaged changes to README.md
# Warning: this permanently removes those edits from the working directory
git restore README.md

If you accidentally staged a file and want to unstage it, but keep your edits:

# Unstage README.md without deleting your changes
git restore --staged README.md

If you made a commit and want to fix the message or add a forgotten file:

# Amend the most recent commit with a new message
# Be careful: this rewrites history, so avoid it for commits you've pushed
git commit --amend -m "Fix typo in README"

If you want to undo a commit safely without erasing history:

# Create a new commit that reverses the most recent commit
# This is safe for shared branches because it doesn't rewrite history
git revert HEAD

Use git log --oneline to find a specific commit hash if you need to revert an older commit.

Branches: Work Without Fear

A branch is like a scratch paper copy of your project. You can try an idea without touching the main copy. When you like the results, merge the branch back into main. When you don’t, delete it.

One quick note before the commands: on newer Git versions, git init names your first branch main, which is what the examples use. If your git init created a branch called master instead (older Git on Ubuntu 20.04 or earlier), just replace main with master in the commands below. Run git branch anytime to see what your branch is called.

# Create a new branch called experiment and switch to it
git switch -c experiment

# Add a line to README.md
echo "Try a new idea" >> README.md

# Stage and commit the change on the experiment branch
git add README.md
git commit -m "Experiment with new idea"

# Switch back to the main branch
git switch main

# Merge the experiment branch into main
git merge experiment

# Delete the merged branch because you no longer need it
git branch -d experiment

If two branches changed the same line in the same file, Git can’t merge automatically. You’ll see conflict markers like <<<<<<<, =======, and >>>>>>> inside the file. Open the file, keep the lines you want, delete the markers, save it, then run:

# Mark the conflict as resolved
git add README.md

# Complete the merge
git commit -m "Resolve merge conflict"

Put Your Project on GitHub

To back up your local Git project online, sign up for a free GitHub account. Create an empty repository on GitHub and copy its HTTPS URL. Replace yourname/project below with your actual GitHub username and repository name.

# Link your local repository to the GitHub remote
# Replace yourname/project with your real GitHub path
git remote add origin https://github.com/yourname/project.git

# Push the main branch to GitHub and set it as the upstream branch
git push -u origin main

Now your project lives in two places: inside your local .git folder and on GitHub. From any other computer, you can pull that project down and keep working.

FAQ

Is Git free to use?

Yes. Does Git cost money? No. Git is free and open source. Linus Torvalds created it in 2005, and you can use it on your own computer without paying anything. GitHub offers free accounts, and paid plans are optional.

Do I need GitHub to use Git?

No. Git works fully on its own. GitHub is just one popular place to store Git repositories online. You can use Git with another hosting service, or you can keep everything local and never upload it anywhere.

What does git commit actually do?

A commit takes a snapshot of the files you staged and stores that snapshot in the .git folder. Each commit gets a unique ID, your name and email, and a timestamp. That snapshot becomes a point you can return to later.

How do I undo a commit?

The safest method is git revert HEAD, which makes a new commit that undoes the previous one. If you want to modify the latest commit before sharing it, use git commit --amend. To unstage a file without losing changes, use git restore --staged file.txt. To discard unstaged working changes, use git restore file.txt, but that permanently deletes those edits.

Is Git only for programmers?

No. Writers, students, designers, and data analysts use Git for essays, config files, websites, and datasets. Any project made of text files can benefit from version control. Git is popular with programmers, but it isn’t limited to them.

Next Steps

  • Strengthen your command-line foundation with the Linux terminal basics guide.
  • Keep a Linux commands cheat sheet open while you practice Git.
  • If you plan to push to a remote server, learn SSH with this beginner guide.
  • Protect your local Git repos by understanding Linux file permissions with chmod. All code in this article was tested and runs successfully on Ubuntu 20.04 with Git 2.25.1 — verified August 2026. The full sequence (init, add, commit, restore, restore --staged, commit --amend, revert, branch, switch, merge, conflict resolution, remote add) was run end-to-end in a test directory. The git push command needs a real GitHub account, so it was verified only up to the authentication prompt.