Git commands cheat sheet
Every example was run by our test suite in a fresh repository that already contains one commit (readme.txt containing "hello", message "Add readme", branch main). Commit hashes in your repository will differ. Commands chained with ; run one after another. For explanations, follow the Git track.
Setup commands run before each example
git init -q
printf "hello\n" > readme.txt
git add readme.txt
git commit -q -m "Add readme"Setup and statusStaging and committingHistoryBranches and mergingUndoing and stashingRemotes
Setup and status
| Syntax | What it does | Example → result |
|---|---|---|
git config user.name | Show (or set) your name for commits | git config user.name→ Ethan Cole |
git status --short | Compact status: ?? untracked, A added, M modified | echo x > new.txt; git status --short→ ?? new.txt |
git ls-files | Files Git is tracking | git ls-files→ readme.txt |
git check-ignore path | Is this path ignored by .gitignore? | echo "*.log" > .gitignore; git check-ignore app.log→ app.log |
Staging and committing
| Syntax | What it does | Example → result |
|---|---|---|
git add . | Stage every change below this folder | echo x > b.txt; git add .; git status --short→ A b.txt |
git commit -am "msg" | Stage tracked files and commit | echo more >> readme.txt; git commit -qam "Update"; git rev-list --count HEAD→ 2 |
git mv old new | Rename a tracked file | git mv readme.txt README.md; git status --short→ R readme.txt -> README.md |
git diff --shortstat | One-line summary of unstaged changes | echo more >> readme.txt; git diff --shortstat→ 1 file changed, 1 insertion(+) |
History
| Syntax | What it does | Example → result |
|---|---|---|
git log --oneline | One line per commit | git log --oneline→ 1a2b3c4 Add readme |
git log -1 --format=%s | Subject of the latest commit | git log -1 --format=%s→ Add readme |
git log -1 --format=%an | Author of the latest commit | git log -1 --format=%an→ Ethan Cole |
git rev-list --count HEAD | Number of commits | git rev-list --count HEAD→ 1 |
git cat-file -t ref | Type of a Git object | git cat-file -t HEAD→ commit |
Branches and merging
| Syntax | What it does | Example → result |
|---|---|---|
git branch --show-current | Name of the current branch | git branch --show-current→ main |
git switch -c name | Create a branch and switch to it | git switch -c feature→ Switched to a new branch 'feature' |
git branch | List branches (* = current) | git branch→ * main |
git merge name | Merge a branch into the current one | git switch -q -c f; echo x > f.txt; git add f.txt; git commit -qm "Add f"; git switch -q main; git merge -q f; git log -1 --format=%s→ Add f |
git tag name | Mark the current commit | git tag v1.0; git tag→ v1.0 |
Undoing and stashing
| Syntax | What it does | Example → result |
|---|---|---|
git restore file | Discard uncommitted edits to a file | echo oops > readme.txt; git restore readme.txt; cat readme.txt→ hello |
git restore --staged file | Unstage, keep the edit | echo x > b.txt; git add b.txt; git restore --staged b.txt; git status --short→ ?? b.txt |
git reset --soft HEAD~1 | Undo the last commit, keep it staged | echo b >> readme.txt; git commit -qam "Two"; git reset -q --soft HEAD~1; git status --short→ M readme.txt |
git revert --no-edit ref | New commit that reverses ref | echo b >> readme.txt; git commit -qam "Two"; git revert --no-edit HEAD >/dev/null; git log -1 --format=%s→ Revert "Two" |
git stash | Set uncommitted work aside | echo wip >> readme.txt; git stash -q; git stash list→ stash@{0}: WIP on main: 1a2b3c4 Add readme |
Remotes
| Syntax | What it does | Example → result |
|---|---|---|
git remote add name url | Connect a remote | git remote add origin https://example.com/demo.git; git remote→ origin |
git remote get-url name | Address of a remote | git remote add origin https://example.com/demo.git; git remote get-url origin→ https://example.com/demo.git |