MSSamara Stack

Home › Articles

Git for beginners: the essential commands you actually need

Moath Samara · Published October 26, 2026

Git is the tool that records your code's history, lets you go back, and lets you work with others without losing anyone's changes. You do not need to memorise hundreds of commands. These few cover most of your daily work.

Based on the official Pro Git book and the GitHub docs. The switch and restore commands are available from Git 2.23 onward.

What does Git do?

It saves snapshots (commits) of your project over time. You edit files, choose what goes into the snapshot, and record it with a message describing what you did. Your changes pass through three stages: the working folder, the staging area, then the repository.

Setup (once)

git --version
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

The basic loop

git init                    # start a repository in this folder
git status                  # what changed?
git add .                   # stage all changes
git commit -m "Add homepage"  # record the snapshot
git log --oneline           # view the history

Write short, specific messages describing what changed. They will help you in a month when you are hunting for one particular change.

Branches: experiment without breaking your work

git switch -c feature-search   # new branch, and switch to it
# ... edit and commit ...
git switch main
git merge feature-search
git branch                    # list branches

The default branch may be main or master depending on your setup, so check with git branch.

Working with GitHub

git remote add origin https://github.com/USER/REPO.git
git push -u origin main     # first push
git pull                     # fetch other people's changes
git clone https://github.com/USER/REPO.git   # copy an existing repository

Undoing things safely

The .gitignore file and your secrets

node_modules/
.env
*.log

Never commit passwords or API keys. If you commit a key by mistake, treat it as exposed: change it immediately, because deleting the file does not erase it from the repository history.

Common errors

Message or symptomCauseFix
not a git repositoryYou are not inside a project folderMove to the right folder or run git init
Push rejectedThe remote has changes you do not haveRun git pull, then try again
You forgot a file in the commitIt was not staged with git addgit add file then git commit --amend (before pushing only)
A secret key went into a commitIt was pushed with the codeChange the key first, before anything else

A 15-minute exercise

  1. Create a new folder and run git init.
  2. Create README.md with one sentence, then git add . and git commit.
  3. Create a branch named edit-readme, edit the file and commit.
  4. Go back to main and merge the branch.
  5. Create an empty repository on GitHub, connect it and push with git push -u origin main.

How to practise more

Try the interactive GitHub Skills lessons, and go back to Pro Git when you need detail. Our library has a “Web developer from scratch” path that puts Git in the right place.

FAQ

What is the difference between Git and GitHub?

Git is a tool on your machine that records history. GitHub is an online service that hosts your repositories and lets people collaborate on them.

Do I need the command line?

No. GitHub Desktop offers the basic loop in a graphical interface, but understanding the commands helps when things go wrong.

A new tool every day

I post a free AI tool on X every day, and all sources and paths live in the library.

Follow on X   Open the library

Sources