Advertisement
Advertisement
How-to

The Git Commands You'll Actually Use (A Beginner's Cheat Sheet)

Git has hundreds of commands. You need about twelve. Here they are, in the order you'll use them, with plain-English explanations.

Green terminal text on a dark screen
Photo: Jake Walker on Unsplash

Git has a reputation for being confusing, and honestly, it's earned. But day to day, almost everyone uses the same dozen commands. Learn these and you'll handle 95% of what comes up.

Think of Git as a save system for your project, where every save (a commit) is a snapshot you can go back to.

Setting up (once per computer)

git config --global user.name "Your Name"
git config --global user.email "you@example.com"

Tells Git who you are. These details appear on every commit you make.

Advertisement

Starting a project

git init

Turns the current folder into a Git project. Run it once, inside your project folder.

git clone https://github.com/someone/project.git

Downloads a copy of an existing project, with its full history.

The everyday loop

This is the cycle you'll repeat hundreds of times.

git status

Shows what's changed since your last commit. Run this constantly. It answers "wait, what state am I in?" before you do anything else.

git add file.txt
git add .

Chooses which changes go into the next snapshot. git add . adds everything in the current folder.

git commit -m "Add contact form"

Saves the snapshot with a short message describing it. Good messages say what changed: "Fix login button on mobile" beats "stuff".

Advertisement
git push

Uploads your commits to GitHub (or wherever your remote copy lives).

git pull

Downloads other people's commits and merges them into your copy. Do this before you start work each day.

Working on something new: branches

A branch lets you work on a feature without touching the main version until it's ready.

git switch -c new-feature

Creates a branch called new-feature and moves you onto it.

git switch main

Goes back to the main branch.

git merge new-feature

Brings the finished work from new-feature into the branch you're on.

Looking back

git log --oneline

Shows the history, one commit per line. Press q to quit.

git diff

Shows exactly what you've changed but not committed yet.

Undo buttons (the ones that won't hurt you)

git restore file.txt

Throws away your uncommitted changes to a file and goes back to the last commit. Careful: those changes are gone.

git restore --staged file.txt

Un-adds a file you git added by mistake. Your changes stay.

git commit --amend -m "Better message"

Fixes the message of your last commit. Only do this before you push.

git revert <commit-id>

Creates a new commit that undoes an old one. It's the safe way to undo something that's already been pushed, because it doesn't rewrite history.

When you see a merge conflict

Don't panic. It just means two people changed the same lines. Git marks the clash in the file like this:

<<<<<<< HEAD
your version
=======
their version
>>>>>>> other-branch

Edit the file to keep what's right, delete the marker lines, then git add the file and git commit. That's all a merge conflict is.

The one habit that saves you

Commit small and often. One commit per logical change. Small commits are easy to understand, easy to undo and make conflicts rare.

Using VS Code? The built-in Source Control panel does most of this with buttons, and the GitLens extension makes history much easier to explore. See 10 VS Code extensions worth installing.

Sponsored

Read next