Programming and IT

Git commands

Git has well over a hundred commands, but on an ordinary day you use about twenty. Here they are in the order you need them — from your first copy of a repository to undoing a bad step — with one line of explanation each.

Updated
In this article

How this cheat sheet is organised#

The commands are grouped not alphabetically but by when you need them: create or get a repository, live through a working day in it, split work into branches, exchange with a server, fix a mistake. Within each group, the order is the one in which you usually type them.

Almost every command has the same skeleton: git — command name — options — the things to apply it to. If you forget an option, help is close by: git help commit opens the full manual page, and git commit -h prints a short list of options right in the terminal.

Getting started#

Command What it does
git init turns an ordinary folder into a repository by creating the hidden .git directory
git clone <url> fetches an existing repository from a server with its whole history
git config --global user.name sets the author name that goes into every commit
git remote -v shows which servers the repository is connected to

The first steps are covered in how to create a Git repository, and copying an existing one in the article on git clone, including the --depth and --branch options and what to do when cloning fails.

The everyday loop#

Command What it does
git status lists modified, staged and unknown files
git add <file> puts the current state of a file into the index, for the next commit
git diff shows edits that are not in the index yet
git diff --staged shows what is already selected and will go into the commit
git commit -m "text" records the contents of the index in history as a commit
git log --oneline -10 the last ten commits, one line each
git show <hash> shows one commit in full: message and changes
git restore <file> puts a file back to its state in the last commit

These eight cover most of the work. If you learn only one of them, make it git status: it answers the question "what is going on right now" and almost always hints at the next command. Recording history is covered in detail in git commit, and reading it in git log.

Branches and merging#

Command What it does
git branch lists branches; the current one is marked with an asterisk
git switch <branch> switches to another branch
git switch -c <branch> creates a branch and switches to it at once
git merge <branch> merges the named branch into the current one
git stash sets unfinished edits aside so you can switch with a clean slate

Working with branch pointers is explained in the article on git branch — including detached HEAD, which is easy to end up in by accident. Joining histories and resolving conflicts is the topic of git merge, and setting work aside of git stash.

Talking to the server#

Command What it does
git fetch downloads other people's commits without touching your files
git pull downloads them and merges them into the current branch right away
git push sends your commits to the remote repository
git push -u origin <branch> pushes a new branch and links it to the server branch
git tag -a v1.0 -m "text" puts a named label on a commit

The difference between downloading and merging is the topic of the article on git pull, and server rejections on push are covered in the one on git push.

Fixing mistakes#

Command What it does
git restore --staged <file> unstages a file without touching the file itself
git reset --soft HEAD~1 removes the last commit, keeping the edits staged
git revert <hash> creates a new commit that cancels the given one
git reflog lists previous positions of HEAD — this is how you find "lost" work
git rm --cached <file> stops tracking a file while keeping it on disk

Of the whole list, only git reset --hard is truly dangerous: it erases uncommitted edits with no way to get them back. Anything that made it into at least one commit can almost always be recovered through git reflog, even if the branch was deleted. The full picture is in how to undo a commit.

Where to start if all of this is new#

Create a practice repository with three text files and go through the whole loop: init, add, commit, a branch, an edit in the branch, a merge, a deliberate conflict, an undo. Half an hour of this gives you more than rereading a list of commands, because what sticks is not the text but the sequence.

Next, add a .gitignore file — without one a project gets noisy fast. And get into the habit of running git status and git log --oneline -5 before every command that changes history: two seconds of checking save an evening of untangling.

Step-by-step plan

  1. Set your name and emailgit config --global user.name and user.email — once per computer.
  2. Get a repositorygit init for a new project or git clone for an existing one.
  3. Master the daily loopstatus, add, commit, log — until you stop peeking at the cheat sheet.
  4. Add branchesswitch -c, work, merge; create a conflict on purpose and resolve it.
  5. Connect a serverpush -u, then pull before every push.
  6. Learn the undo commandsrestore, reset --soft, revert, reflog — before you need them in a hurry.

Start learning this in your own space

The plan goes into your repository: tick off stages, keep notes — the change history shows how far you have come.

Start the plan

Check yourself

1.Which command shows which files are modified and what is already selected for the commit?

2.You staged a file by mistake. How do you remove it from the index without losing your edits?

3.Which command helps you find the commits of a deleted branch?

4.What does git fetch do, unlike git pull?

Sources

Was this helpful?

More articles

Programming and IT Linux commands The Linux terminal works the same way in every distribution: command name, options, arguments. Below are six tables by area of work, each with one line per command and an example you can type to see the result straight away. Programming and IT Regular expressions A regular expression is a compact description of a set of strings. The same syntax works in grep, in your code editor, in JavaScript, PHP, Java and Python, so you only need to learn it once. This page covers the pattern language itself, not the library of any particular language. Programming and IT Markdown tables A Markdown table is built from pipes and a separator line under the header. Below is the table syntax with column alignment, plus a short cheat sheet for the rest of the markup — headings, lists, links, images, code, quotes and task lists. Programming and IT How to learn Python from scratch Python is a good first programming language: code reads almost like text, and the standard library covers most everyday tasks. This plan takes you from installing the interpreter to your own scripts covered by tests in about four months, at roughly an hour a day. Programming and IT How to learn SQL from scratch SQL is the query language of relational databases. Developers, analysts, testers and managers who want to pull numbers themselves all need it. Basic queries take a few weeks to learn; working confidently with complex reports takes two or three months of practice. Below is the order of topics and ways to train on a real database. Programming and IT How to learn Linux from scratch Linux runs most servers, containers and countless devices, so developers, testers, analysts and system administrators all need the command line. The easiest way to learn it is not by reading lists of commands but by working in the terminal every day and solving small practical tasks. Below is a sequence of topics for two to three months.

More solutions