SpunkArt
Command line and project setup

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

  1. Working tree. The files on disk that you edit.
  2. Staging area (also called the index). The snapshot you are preparing for the next commit.
  3. Repository. The history of commits stored in the .git folder.

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

CommandUse
git initTurn the current folder into a repository
git clone <address>Copy an existing repository and its history
git statusShow changed, staged and untracked files
git diffChanges not yet staged
git diff --stagedChanges staged for the next commit
git log --oneline --graphCompact history with branch lines
git show <commit>One commit’s message and changes
git blame fileWhich 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

CommandUse
git switch -c feature/searchCreate a branch and move to it
git switch mainMove to an existing branch
git branchList local branches
git merge feature/searchBring that branch into the current one
git rebase mainReplay your commits on top of the latest main
git branch -d feature/searchDelete 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

SituationCommandSafe after push?
Throw away edits to one filegit restore fileNot relevant (local only)
Unstage a file, keep the editsgit restore --staged fileNot relevant
Undo a commit with a new opposite commitgit revert <commit>Yes
Move the branch back, keep changes stagedgit reset --soft HEAD~1No
Move the branch back and discard changesgit reset --hard HEAD~1No
Park unfinished workgit stash, later git stash popNot 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.