15 Essential Git Commands

The commands you will actually use, ranked by importance, with one-click copy.

15 commands, ranked by use
10 bonus commands
1 click to copy any of them

Start here

01 Clone the repository

git clone https://github.com/user/repo.git

Downloads a full copy of a GitHub repository to your machine, with its entire history and all branches. Run this once, when you first start on a project. Then cd into the new folder and start working. Everything else on this page happens inside it.

Before anything

02 Check your state

git status

The safest command in git. It shows which files you changed, which ones are staged, and which branch you are on. Run it any time you feel lost - it changes nothing and always tells you the truth.

Every morning

03 Pull master to your machine

git pull origin master

Downloads the latest master from GitHub and merges it into your local copy, so your machine matches the server. Run it before you start working each day, and before you merge, so you are not building on stale code.

Prepare files

04 Stage one file

git add index.html

Stages a single file, marking it ready for the next commit. This is also how you push only one file: git add that file, commit it, and push. To stage everything at once, use git add -A.

Checkpoint

05 Save a snapshot

git commit -m "Add feature"

Saves everything you staged as one permanent snapshot, with a short message explaining what you did. The message is what your teammates and future you will read. The -m flag writes the message inline, in quotes.

Publish

06 Push to master

git push origin master

Uploads your new commits to the master branch on GitHub. Until you push, your work exists only on your own machine. This is the step that publishes your changes to the server, where the world, and your team, can see them.

New feature

07 Branch off master

git checkout -b feature/login

Creates a new branch off master and switches to it in one step. Build every new feature on its own branch so master always stays clean and deployable. Name branches after the feature, like feature/login or feature/search.

Publish the branch

08 Push your new branch

git push origin feature/login

Uploads your new branch, with all its commits, to GitHub for the first time. GitHub creates the branch on their end. This is how you share new code without touching master, and how you open a pull request.

Go back

09 Switch back to master

git checkout master

Switches your working folder back to the master branch. Do this once your feature is finished and committed, and you are ready to bring it into master. Your feature branch stays exactly as you left it.

Ship it

10 Merge into master

git merge feature/login

While you are on master, this pulls every commit from your feature branch into master. The full flow: checkout master, merge your branch, then git push origin master to publish. The merge stays local until you push.

Know where you are

11 List your branches

git branch

Lists every branch on your machine and marks the one you are on with a star. Add -a to also see the branches that exist on GitHub. Use it to confirm where you are before switching or merging anything.

Read history

12 Read the story

git log --oneline

Shows your project history as one compact line per commit: short hash, message, and author. Press q to exit. It is the fastest way to see what happened, when it happened, and who did it.

Review first

13 See what changed

git diff

Shows the exact line-by-line changes in your files since the last commit, before you commit them. Use git diff --staged to review what you already staged. It is read-only, and a good habit before every commit.

Look, don't touch

14 Peek at the remote

git fetch origin

Downloads new commits and branches from GitHub without merging anything into your work. Think of it as pull without touching your files. Inspect what changed first; merge later, when you are ready.

Freeze work

15 Shelve your changes

git stash

Temporarily shelves your uncommitted changes and gives you a clean working folder, so you can switch branches or pull safely. Run git stash pop later to bring the work back. The perfect pocket for work in progress.

The extended kit

10 More commands you might need

git init

Turns the current folder into a brand new git repository.

git remote add origin <url>

Connects a local repo to an empty repo on GitHub so you can push to it.

git switch feature/login

The modern way to move to an existing branch. Add -c to create it.

git stash pop

Restores the changes you shelved with git stash.

git restore index.html

Discards uncommitted changes in one file. Not undoable, so look first.

git push -u origin feature/login

Pushes a new branch and links it upstream, so later pushes need no arguments.

git branch -d feature/login

Deletes a local branch after you have merged it.

git reset --hard origin/master

Forces local master to exactly match GitHub. Destructive; throws local work away.

git revert abc1234

Undoes an earlier commit by adding a new one. The safe, history-preserving undo.

git rm --cached secrets.env

Stops tracking a file but keeps it on disk - the fix for accidentally committed files.