# 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.

## 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](https://skillok.org/topics/how-to-create-a-git-repository),
and copying an existing one in the article on [git clone](https://skillok.org/topics/git-clone-command),
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](https://skillok.org/topics/git-commit-command), and reading it in
[git log](https://skillok.org/topics/git-log-command).

## 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](https://skillok.org/topics/git-branches) — including detached HEAD, which is easy to end up
in by accident. Joining histories and resolving conflicts is the topic of
[git merge](https://skillok.org/topics/git-merge-command), and setting work aside of
[git stash](https://skillok.org/topics/git-stash-command).

## 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](https://skillok.org/topics/git-pull-command), and server rejections on push are covered in
the one on [git push](https://skillok.org/topics/git-push-command).

## 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](https://skillok.org/topics/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](https://skillok.org/topics/gitignore-file) 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 email** — git config --global user.name and user.email — once per computer.
2. **Get a repository** — git init for a new project or git clone for an existing one.
3. **Master the daily loop** — status, add, commit, log — until you stop peeking at the cheat sheet.
4. **Add branches** — switch -c, work, merge; create a conflict on purpose and resolve it.
5. **Connect a server** — push -u, then pull before every push.
6. **Learn the undo commands** — restore, reset --soft, revert, reflog — before you need them in a hurry.

## Check yourself

1. Which command shows which files are modified and what is already selected for the commit?
   - git status
   - git log
   - git show
2. You staged a file by mistake. How do you remove it from the index without losing your edits?
   - git restore --staged file
   - git rm file
   - git reset --hard
3. Which command helps you find the commits of a deleted branch?
   - git reflog
   - git clean
   - git tag
4. What does git fetch do, unlike git pull?
   - Only downloads commits without changing the current branch
   - Sends commits to the server
   - Creates a new branch from the server one

## Sources

- [Pro Git book](https://git-scm.com/book/en/v2) — A complete textbook; the first three chapters cover this whole cheat sheet
- [Git reference](https://git-scm.com/docs) — The official manual page for every command
- [GitHub Git Cheat Sheet](https://education.github.com/git-cheat-sheet-education.pdf) — A printable one-page summary

---

https://skillok.org/articles/en/git-commands-cheat-sheet
