Git cheat sheet for everyday work
Git commands make more sense once you know where each one moves your changes. This page starts with that picture, then lists the commands you will use most, grouped by task, with the undo commands kept apart.
Three places your changes live
- Working tree. The files on disk that you edit.
- Staging area (also called the index). The snapshot you are preparing for the next commit.
- Repository. The history of commits stored in the
.gitfolder.
git add copies changes from the working tree to the staging area. git commit turns the staging area into a commit. git status tells you what is in each place right now, and it is the command to run whenever you are unsure.
Start and inspect
| Command | Use |
|---|---|
git init | Turn the current folder into a repository |
git clone <address> | Copy an existing repository and its history |
git status | Show changed, staged and untracked files |
git diff | Changes not yet staged |
git diff --staged | Changes staged for the next commit |
git log --oneline --graph | Compact history with branch lines |
git show <commit> | One commit’s message and changes |
git blame file | Which commit last touched each line |
Save work
git add index.html # stage one file
git add -p # stage chosen parts of files, hunk by hunk
git commit -m "Fix footer link on mobile"
git commit --amend # change the last commit (before pushing)
Write the message as a short line in the imperative, as if completing the sentence “This commit will…”. Add a blank line and a longer body when the reason is not obvious.
Branches
| Command | Use |
|---|---|
git switch -c feature/search | Create a branch and move to it |
git switch main | Move to an existing branch |
git branch | List local branches |
git merge feature/search | Bring that branch into the current one |
git rebase main | Replay your commits on top of the latest main |
git branch -d feature/search | Delete a merged branch |
Older guides use git checkout for both switching branches and restoring files. The newer switch and restore commands split those jobs so each name says what it does.
Work with a remote
git remote -v # list remotes
git fetch origin # download new commits, change nothing locally
git pull # fetch, then merge into the current branch
git pull --rebase # fetch, then rebase instead of merging
git push -u origin feature/search # first push of a new branch
Undo, from gentle to drastic
| Situation | Command | Safe after push? |
|---|---|---|
| Throw away edits to one file | git restore file | Not relevant (local only) |
| Unstage a file, keep the edits | git restore --staged file | Not relevant |
| Undo a commit with a new opposite commit | git revert <commit> | Yes |
| Move the branch back, keep changes staged | git reset --soft HEAD~1 | No |
| Move the branch back and discard changes | git reset --hard HEAD~1 | No |
| Park unfinished work | git stash, later git stash pop | Not relevant |
If you lose a commit after a reset, git reflog lists where HEAD has pointed recently. Find the old hash and run git switch -c rescue <hash>.
Mistakes to avoid
- Rewriting shared history. Amend, rebase and reset change commit hashes. On a branch other people use, prefer
revert. - Committing secrets. Deleting the file in a later commit leaves the secret in history. Rotate the key at once, then clean history if needed.
- Force pushing blindly. If you must, use
git push --force-with-lease, which refuses when the remote has commits you have not seen.
Questions
What is the difference between fetch and pull?
Fetch only downloads. Pull downloads and then merges or rebases into your current branch, so it can change your files.
What does detached HEAD mean?
You checked out a commit rather than a branch. New commits there belong to no branch. Create one with git switch -c name to keep them.