Git for beginners: the essential commands you actually need
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.
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 historyWrite 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 branchesThe 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 repositoryUndoing things safely
- git restore file: discards changes to a file that you have not committed. You cannot recover them afterwards.
- git restore --staged file: takes the file out of staging without touching your edits.
- git revert <commit>: creates a new commit that reverses an earlier one without rewriting history, which is the safest choice after pushing.
- Warning: git reset --hard deletes your uncommitted changes. Do not use it until you understand it.
The .gitignore file and your secrets
node_modules/
.env
*.logNever 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 symptom | Cause | Fix |
|---|---|---|
| not a git repository | You are not inside a project folder | Move to the right folder or run git init |
| Push rejected | The remote has changes you do not have | Run git pull, then try again |
| You forgot a file in the commit | It was not staged with git add | git add file then git commit --amend (before pushing only) |
| A secret key went into a commit | It was pushed with the code | Change the key first, before anything else |
A 15-minute exercise
- Create a new folder and run git init.
- Create README.md with one sentence, then git add . and git commit.
- Create a branch named edit-readme, edit the file and commit.
- Go back to main and merge the branch.
- 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.