Skip to content
InfoGraphHub

Git Commands Cheat Sheet: The Essentials for Beginners

The Git commands beginners use most: set up a repository with init or clone, save work with add and commit, build features on branches with switch and merge, share work with fetch, pull and push, and undo safely with restore.

Git cheat sheet in five groups: starting a project (init, clone, config), saving changes (status, add, commit, diff), branches (branch, switch, merge), remotes (fetch, pull, push) and history and undo.
InfoGraphHub · infographhub.com · CC BY-NC 4.0

What Git does

Git is a version control system: it records snapshots of your project so you can see what changed, when and by whom, and go back if something breaks. Instead of keeping files named essay_final_v2.docx, you keep one folder and let Git remember every saved version.

Git thinks about your work in three places:

  • the working tree, the files you are editing now;
  • the staging area (also called the index), where you collect the changes for your next snapshot;
  • the repository, where each snapshot, called a commit, is stored for good.

Starting and saving

Run git init inside a folder to start a new repository, or git clone <url> to copy an existing one, including its full history. On a new computer, set your name and email once with git config --global, because every commit records who made it.

The everyday loop is status → add → commit. git status shows which files are changed, staged or untracked. git add stages the exact content you want in the next commit; the Pro Git book suggests thinking of it as “add precisely this content to the next commit”. git commit -m "Message" then saves the snapshot. Use git diff to see edits you have not staged yet.

Working on branches

A branch is a separate line of work. You can try a new feature on its own branch without touching the working version.

git switch -c <branch> creates a branch and moves you onto it, and git switch <branch> moves between existing branches. When the work is ready, switch back to your main branch and run git merge <branch> to bring the changes in. The switch and restore commands arrived in Git 2.23; older tutorials use git checkout for the same jobs, and it still works.

Sharing with a remote

A remote is a copy of the repository on a server such as GitHub or GitLab. When you clone, Git names that remote origin by default.

  • git fetch downloads new commits from the remote but leaves your files and branches alone.
  • git pull runs a fetch and then updates your current branch. In current versions of Git it only fast-forwards by default: if you and the remote both have new commits, it stops and asks you to choose a merge or a rebase.
  • git push uploads your commits. Adding -u the first time links your branch to the remote one, so later you can type just git push or git pull.

Worked example: a hackathon in Bengaluru

Ananya and Farhan are building a website for a college hackathon in Bengaluru. Ananya runs git init, writes index.html, then runs git add index.html and git commit -m "Add homepage". She pushes it to a new GitHub repository with git push -u origin main.

Farhan runs git clone with the repository’s URL, then git switch -c styling to work on the CSS without disturbing the homepage. When he is done, he pushes his branch, and after the team checks it, the styling branch is merged into main. Ananya runs git pull to get the finished design.

If Farhan stages a file by mistake, git restore --staged style.css (or the short form -S) takes it out of the next commit. Be careful with plain git restore <file>: it throws away unsaved edits for good.

Frequently asked questions

What is the difference between git fetch and git pull?

git fetch only downloads new commits from the remote and updates your remote-tracking branches, such as origin/main. git pull runs a fetch and then brings those changes into your current branch.

What does staging mean in Git?

Staging means choosing which changes go into your next commit. git add copies a file’s current content into the staging area, so you can commit some changes and leave others for later.

Should I use git switch or git checkout?

Both work. git switch, added in Git 2.23, does only one job, changing branches, which makes it easier to learn. git checkout -b <branch> does the same as git switch -c <branch>.

What does origin mean?

origin is the default name Git gives to the remote repository you cloned from. It is just a short nickname for the server’s URL, and you can rename it if you like.

Sources & methodology

Every fact is checked against the sources below. We write original explanations and draw original graphics; no figures are copied from textbooks. Spotted an error? See our corrections policy.

  1. Pro Git: Git Basics – Recording Changes to the Repository (git-scm.com, accessed 30 Sept 2026)
  2. git-switch Documentation (git-scm.com, accessed 30 Sept 2026)
  3. git-pull Documentation (git-scm.com, accessed 30 Sept 2026)
  4. git-restore Documentation (git-scm.com, accessed 30 Sept 2026)

Reviewed by Claude (pending owner check) · Updated

Embed this on your site

Paste this HTML into your page. Keep the credit line: it is required by the CC BY-NC 4.0 license.

Get new InfoGraphHub resources by email

One short email when new free resources go live. No spam, unsubscribe anytime.