Git for Beginners: The Commands You Will Actually Use
Git has hundreds of commands, but daily work needs about a dozen. Learn those, with plain explanations and safe ways to undo mistakes.
Key points
- Git saves snapshots of your project called commits, so you can always go back.
- The daily loop is: edit,
git status,git add,git commit,git push. - Branches let you try changes without breaking the main version.
- Check
git statusbefore and after every command while you are learning.
On this page
Git is a version control system that records snapshots of your project over time. As a beginner you only need a small set of commands: init or clone to start, status, add and commit to save work, branch and switch to try ideas safely, and pull and push to sync with a remote such as a hosted repository.
This guide explains each one in plain language, in the order you will use them.
Why use Git at all?
Without version control, people end up with folders full of files named like final, final-2 and final-really. Git replaces that with one clean folder and a complete, searchable history. You can see what changed, when and why, return to any earlier version, and work with other people on the same files without overwriting each other.
Git is not only for large teams. Solo learners benefit just as much, because it makes experimenting safe: if an idea breaks everything, you go back to the last working commit.
How to set up Git and start a repository
Configure Git once
After installing Git, tell it who you are. This name and email are attached to every commit you make.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch mainCreate or copy a repository
git initturns the current folder into a new repository.git clone <url>downloads an existing repository, including its full history.- A
.gitignorefile lists things Git should never track, such as virtual environments, build output and files containing secrets.
The everyday Git workflow
Most of your time with Git follows the same loop. Files move from your working folder, to the staging area (what will go in the next snapshot), to a commit.
- Edit your files as normal.
- Run
git statusto see what changed. - Run
git diffto review the exact lines you changed. - Stage the files you want with
git add <file>. - Save the snapshot with
git commit -m "Describe the change". - Share it with
git push.
git status
git diff
git add app.py README.md
git commit -m "Add input validation to the signup form"
git pushWorking with branches and remotes
Branches
A branch is a separate line of work. You can experiment on a branch and merge it back only when it works.
| Command | What it does |
|---|---|
git branch | Lists branches and shows which one you are on |
git switch -c new-feature | Creates a new branch and switches to it |
git switch main | Moves back to the main branch |
git merge new-feature | Brings the branch's commits into the current branch |
git branch -d new-feature | Deletes a branch that has already been merged |
Syncing with a remote repository
git pullfetches new commits from the remote and merges them into your branch.git pushuploads your commits.git log --oneline --graphshows a compact history so you can see where branches split and joined.
If two people changed the same lines, Git stops and marks a merge conflict in the file. Open it, keep the correct version between the markers, remove the markers, then git add and git commit to finish.
How to undo mistakes in Git
git restore <file>discards uncommitted changes to a file.git restore --staged <file>removes a file from the staging area but keeps your edits.git commit --amendfixes the message or contents of your last commit, before you push it.git revert <commit>creates a new commit that undoes an earlier one, which is safe for shared history.
Your next step
Create a practice folder, run git init, and make five small commits while watching git status after each one. Then use it for your first projects from our Python roadmap. Keep any account tokens in a password manager, never inside your repository.
Frequently asked questions
What is the difference between Git and a hosted Git service?
Git is the version control tool that runs on your computer. Hosting services store a copy of your repository online so you can back it up and collaborate.
What does git add actually do?
It moves changes into the staging area, which is a list of what will be included in your next commit. Nothing is saved to history until you commit.
What is the difference between git pull and git fetch?
git fetch downloads new commits without changing your files. git pull fetches and then merges them into your current branch.
How often should I commit?
Commit whenever you finish a small, working step. Small, frequent commits are easier to understand and to undo.