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.
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
- Set your name and emailgit config --global user.name and user.email — once per computer.
- Get a repositorygit init for a new project or git clone for an existing one.
- Master the daily loopstatus, add, commit, log — until you stop peeking at the cheat sheet.
- Add branchesswitch -c, work, merge; create a conflict on purpose and resolve it.
- Connect a serverpush -u, then pull before every push.
- 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.
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
-
Pro Git bookA complete textbook; the first three chapters cover this whole cheat sheetfree
-
Git referenceThe official manual page for every commandfree
-
GitHub Git Cheat SheetA printable one-page summaryfree
Was this helpful?